Realistic 3D camera system
3D camera system components
scoped_lock.hpp
Go to the documentation of this file.
1 //
2 // detail/scoped_lock.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_DETAIL_SCOPED_LOCK_HPP
12 #define ASIO_DETAIL_SCOPED_LOCK_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
19 
21 
22 namespace asio {
23 namespace detail {
24 
25 // Helper class to lock and unlock a mutex automatically.
26 template <typename Mutex>
28  : private noncopyable
29 {
30 public:
31  // Tag type used to distinguish constructors.
33 
34  // Constructor adopts a lock that is already held.
36  : mutex_(m),
37  locked_(true)
38  {
39  }
40 
41  // Constructor acquires the lock.
42  explicit scoped_lock(Mutex& m)
43  : mutex_(m)
44  {
45  mutex_.lock();
46  locked_ = true;
47  }
48 
49  // Destructor releases the lock.
51  {
52  if (locked_)
53  mutex_.unlock();
54  }
55 
56  // Explicitly acquire the lock.
57  void lock()
58  {
59  if (!locked_)
60  {
61  mutex_.lock();
62  locked_ = true;
63  }
64  }
65 
66  // Explicitly release the lock.
67  void unlock()
68  {
69  if (locked_)
70  {
71  mutex_.unlock();
72  locked_ = false;
73  }
74  }
75 
76  // Test whether the lock is held.
77  bool locked() const
78  {
79  return locked_;
80  }
81 
82  // Get the underlying mutex.
83  Mutex& mutex()
84  {
85  return mutex_;
86  }
87 
88 private:
89  // The underlying mutex.
90  Mutex& mutex_;
91 
92  // Whether the mutex is currently locked or unlocked.
93  bool locked_;
94 };
95 
96 } // namespace detail
97 } // namespace asio
98 
100 
101 #endif // ASIO_DETAIL_SCOPED_LOCK_HPP
scoped_lock(Mutex &m, adopt_lock_t)
Definition: scoped_lock.hpp:35