A wrapper class for pthread mutexes which provides a recursive mutex. More...
|A scoped locking class for exception safe RecMutex locking. More...|
|A scoped locking class for exception safe RecMutex locking which tracks the status of its mutex. More...|
|RecMutex (const RecMutex &)=delete|
|RecMutex &||operator= (const RecMutex &)=delete|
|int||lock () noexcept|
|int||trylock () noexcept|
|int||unlock () noexcept|
|static int||test_support ()|
A wrapper class for pthread mutexes which provides a recursive mutex.
This class can be used interchangeably with threads started with GThread and by this library, as both glib and this library use pthreads underneath on POSIX and other unix-like OSes. It can also in practice be used interchangeably with those started by C++11/14 std::thread and with std::recursive_mutex and similar objects, as in C++11/14 on unix-like OSes these facilities will be built on top of pthreads (for which purpose C++11/14 provides the std::native_handle_type type and std::thread::native_handle() function), or if they are not, they will use the same threading primitives provided by the kernel.
RecMutex objects can be constructed statically as well as dynamically and there is no need to call g_thread_init() before they are constructed, even if glib < 2.32 is used. (If created as a static object in global scope, it will not be possible to catch Thread::MutexError or Thread::RecMutexError thrown by its constructor, but if a static global mutex throws there is nothing that could be done anyway except abort.)
See the comments below on the test_support() method of this class, about how the system's support for native recursive mutexes can be tested at runtime and when a user program is compiled. If glib >= 2.32 is installed, it can be assumed that native recursive mutexes are available, as glib >= 2.32 also uses them.
This class cannot be copied. The copy constructor is deleted.
Initialises the pthread mutex. It is not a cancellation point.
|Cgu::Thread::RecMutexError||Throws this exception if the system does not support recursive mutexes. (If this has been checked beforehand, say by calling test_support(), there should be no need to check for this exception.)|
|Cgu::Thread::MutexError||Throws this exception if initialization of the mutex fails, except because the system does not support recursive mutexes. (It is often not worth checking for MutexError, as it means either memory is exhausted or pthread has run out of other resources to create new mutexes.)|
Destroys the pthread mutex. It is not a cancellation point. It does not throw. Destroying a mutex which is currently locked results in undefined behavior.
Locks the mutex and acquires ownership. Blocks if already locked until it becomes free, unless the calling thread already holds the lock, in which case it increments the lock count and returns immediately. It is not a cancellation point. It does not throw. It is thread safe.
This class cannot be copied. The assignment operator is deleted.
Indicates whether the system supports recursive mutexes. This method does not throw. It is thread safe.
Tries to lock the mutex and acquire ownership, but returns immediately if it is already locked with value EBUSY unless the calling thread already holds the lock, in which case it returns normally and increments the lock count. It is not a cancellation point. It does not throw. It is thread safe.
Unlocks a locked mutex owned by the calling thread and either relinquishes ownership (if the mutex has not been recursively locked) or decrements the lock count (if it has). It is not a cancellation point. It does not throw. It must be called by the thread which owns the mutex.