c++-gtk-utils
|
A wrapper class for pthread mutexes. More...
#include <c++-gtk-utils/mutex.h>
Classes | |
class | Lock |
A scoped locking class for exception safe Mutex locking. More... | |
class | TrackLock |
A scoped locking class for exception safe Mutex locking which tracks the status of its mutex. More... | |
Public Member Functions | |
Mutex (const Mutex &)=delete | |
Mutex & | operator= (const Mutex &)=delete |
int | lock () noexcept |
int | trylock () noexcept |
int | unlock () noexcept |
Mutex () | |
~Mutex () | |
Friends | |
class | Cond |
A wrapper class for pthread mutexes.
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::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.
Mutex 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 thrown by its constructor, but if a static global mutex throws there is nothing that could be done anyway except abort, and it would show that the pthreads installation is seriously defective.)
|
delete |
This class cannot be copied. The copy constructor is deleted.
|
inline |
Initialises the pthread mutex. It is not a cancellation point.
Cgu::Thread::MutexError | Throws this exception if initialization of the mutex fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new mutexes.) |
|
inline |
Destroys the pthread mutex. It is not a cancellation point. It does not throw. Destroying a mutex which is currently locked or associated with an active condition variable wait results in undefined behavior.
|
inlinenoexcept |
Locks the mutex and acquires ownership. Blocks if already locked until it becomes free. It is not a cancellation point. It does not throw. It is thread safe.
This class cannot be copied. The assignment operator is deleted.
|
inlinenoexcept |
Tries to lock the mutex and acquire ownership, but returns immediately if it is already locked with value EBUSY. It is not a cancellation point. It does not throw. It is thread safe.
|
inlinenoexcept |
Unlocks a locked mutex owned by the calling thread and relinquishes ownership. It is not a cancellation point. It does not throw. It must be called by the thread which owns the mutex.
|
friend |