CARMA C++
ScopedLock.h
1 #ifndef CARMA_UTIL_SCOPED_LOCK_H
2 #define CARMA_UTIL_SCOPED_LOCK_H
3 
4 
5 #include "carma/util/posixErrors.h"
6 
7 
8 namespace carma {
9 namespace util {
10 
11 
44 
45 template< typename M >
46 class ScopedLock {
47  public:
48 
53 
54  explicit ScopedLock( M & m );
55 
56 
59 
60  /* virtual */ ~ScopedLock( );
61 
62  private:
63  // no copying
64  ScopedLock( const ScopedLock & );
65  ScopedLock & operator=( const ScopedLock & );
66 
67  M & m_;
68 };
69 
70 
71 } // namespace carma::util
72 } // namespace carma
73 
74 
75 // ******* Below here is simply implementation *******
76 
77 template< typename M >
78 inline
80 m_( m )
81 {
82  lockMutex( m );
83 }
84 
85 
86 template< typename M >
87 inline
89 try {
90  logIfPosixError( unlockMutexNoThrow( m_ ) );
91 } catch ( ... ) {
92  // just stifle any exceptions
93 
94  return;
95 }
96 
97 
98 #endif
ScopedLock(M &m)
Obtains a lock on the given mutex for the caller&#39;s thread (possibly waiting an indeterminate amount o...
Definition: ScopedLock.h:79
A templated scope class (i.e.
Definition: ScopedLock.h:46
~ScopedLock()
Releases the lock on the mutex (that was given to the constructor) held by the caller&#39;s thread...
Definition: ScopedLock.h:88