|
threadmm::lock_guard< Lock > Class Template Reference
[Utility classes]
#include <threadmm/lock_guard.hh>
Collaboration diagram for threadmm::lock_guard< Lock >:
[legend]List of all members.
Detailed Description
template<typename Lock>
class threadmm::lock_guard< Lock >
Lock_Guard class used to lock mutex's.
- Parameters:
-
| Lock | The kind of locks to use. |
This class is useful for writting thread-safe code. One creates a lock_guard object on the stack on entry into the critical section, passing it a mutex to lock. When the lock_guard object goes out of scope, the mutex is automatically released. This is a fairly well-known trick to use. Here's an example for a class fulfilling a Singleton pattern, were we protect the singleton creation from race conditions in the case mutliple threads call the singleton::instance() member function: #ifdef _REENTRANT
#define MUTEX_TYPE threadmm::mutex
#else
#define MUTEX_TYPE threadmm::mutex_dummy
#endif
template <typename Mutex=MUTEX_TYPE>
class singleton {
private:
static Mutex _mutex;
static singleton* _instance;
public:
singleton() {}
static singleton* instance() {
if (!_instance) {
threadmm::lock_guard g(_mutex);
if (!_instance) _instance = new singleton;
}
return _instance;
}
};
In a single threaded application, the user can pass some dummy mutex class as the template argument, and suffer no penalties. - Examples:
-
barrier_sync.cc, and prod_cons_cond.cc.
Member Typedef Documentation
Constructor & Destructor Documentation
|
Constructor.
Locks the passed lock. See also lock member function. - Parameters:
-
| l | The lock to use with this lock_guard. |
| spin | see lock member function. |
|
|
Constructor.
Locks the passed lock. See also lock member function. - Parameters:
-
| l | The lock to use with this lock_guard. |
| spin | see lock member function. |
|
|
Destructor.
Unlocks the stored lock. |
Member Function Documentation
|
Lock the lock passed to the constructor.
- Parameters:
-
| spin | If true, the guard will loop indefently until it gets a lock (via Lock::try_lock member function). Otherwise, it will attempt a normal lock. |
|
|
Lock the lock passed to the constructor.
- Parameters:
-
| spin | If true, the guard will loop indefently until it gets a lock (via Lock::try_lock member function). Otherwise, it will attempt a normal lock. |
|
|
Unlock the lock passed to the constructor.
|
Member Data Documentation
|
Flag.
True if lock was released (by unlock member function) before the deconstructor was reached (scope ended). |
The documentation for this class was generated from the following file:
Top of page
Last update Tue Nov 9 12:40:51 2004
Christian Holm
Created by DoxyGen 1.3.9.1
|