#include <mcllib/MCLock.h>
Inheritance diagram for MCLock:
Public Member Functions | |
bool | isLocked () const |
Test to see if the mutex is locked. | |
void | lock () |
Lock the mutex. | |
MCLock (MCMutex *pMutex, bool locked) | |
Construct a lock, optionally locking the provided mutex. | |
MCLock (MCMutex *pMutex) | |
Construct a lock, locking the provided mutex. | |
bool | testLock () |
Test Lock the mutex (which must be derived from MCTestMutex). | |
void | unlock () |
Unlock the mutex. | |
~MCLock () | |
Unlock the mutex provided at construction if it is locked. |
Lock objects lock a mutex (provided at construction time) and unlock the mutex at destruction. Methods are provided for locking and unlocking the mutex other than at construction/destruction. However the easiest way to use this object is in the automatic mode.
Example.
class C { public: void push_back(int i) { MCLock lck(&mtx); // A. Lock the mutex l.push_back(i); // B. Only one thread will do this } private: MCInMutex mtx; std::list<int> l; }This example shows how the std::list push_back() method can easily be made thread safe (if it isn't in your implementation). At most one thread will be able to execute the push_back() at B. All other threads will be blocked at A. until the thread at B. returns. At this point the MCLock object will be destructed thereby automatically unlocking the mutex. This kind of scoped locking frees the developer from the need to worry about remembering to do an unlock for each lock.
|
Construct a lock, locking the provided mutex.
|
|
Construct a lock, optionally locking the provided mutex.
|
|
Unlock the mutex provided at construction if it is locked. This provides for scoped locking. When the lock object goes out of scope, the mutex is unlocked if it was locked. This prevents mutexes being held onto inadvertently |
|
Test to see if the mutex is locked.
|
|
Lock the mutex. If the mutex is already locked a fatal exception is thrown. This is indicative of an application programming error. lock() waits indefinitely until the mutex becomes available. |
|
Test Lock the mutex (which must be derived from MCTestMutex). If the mutex can be locked it is locked and testLock() returns true. If the mutex cannot be locked testLock() returns false.
|
|
Unlock the mutex. If the mutex is already unlocked a fatal exception is thrown. This is indicative of an application programming error. Unlock() typically frees a single thread waiting to lock() the mutex. However, refer to documentation on individual MCMutex derived classes for further details. |