|
|
The Mutex class is used to protect a section of code so that at any given time only a single thread can perform the protected operation. The Mutex is always recursive in that if the same thread invokes the same mutex lock multiple times, it must release it multiple times. This allows a function to call another function which also happens to use the same mutex lock when called directly.
The Mutex can be used as a base class to protect access in a derived class. When used in this manner, the ENTER_CRITICAL and LEAVE_CRITICAL macros can be used to specify when code written for the derived class needs to be protected by the default Mutex of the derived class, and hence is presumed to be 'thread safe' from multiple instance execution.
pthread_mutex_t _mutex |
Pthread mutex object. This is protected rather than private because some mixed mode pthread operations require a mutex as well as their primary pthread object. A good example of this is the Event class, as waiting on a conditional object must be associated with an accessable mutex. An alternative would be to make such classes "friend" classes of the Mutex.
Mutex () |
The mutex is always initialized as a recursive entity.
~Mutex () |
Destroying the mutex removes any system resources associated with it. If a mutex lock is currently in place, it is presumed to terminate when the Mutex is destroyed.
inline void EnterMutex (void) |
Entering a Mutex locks the mutex for the current thread. This also can be done using the ENTER_CRITICAL macro or by using the ++ operator on a mutex.
See also: LeaveMutex
inline bool TryEnterMutex (void) |
Tries to lock the mutex for the current thread. Behaves like #EnterMutex , except that it doesn't block the calling thread if the mutex is already locked by another thread.
Returns: true if locking the mutex was succesful otherwise false
See also: EnterMutex, LeaveMutex
inline void LeaveMutex (void) |
Leaving a mutex frees that mutex for use by another thread. If the mutex has been entered (invoked) multiple times (recursivily) by the same thread, then it will need to be exited the same number of instances before it is free for re-use. This operation can also be done using the LEAVE_CRITICAL macro or by the -- operator on a mutex.
See also: EnterMutex