Cgu::Thread::RecMutex Class Reference

A wrapper class for pthread mutexes which provides a recursive mutex. More...

`#include <c++-gtk-utils/mutex.h>`

## Classes

class | Lock
| A scoped locking class for exception safe RecMutex locking. More...
class | TrackLock
| A scoped locking class for exception safe RecMutex locking which tracks the status of its mutex. More...

## Public Member Functions

| RecMutex (const RecMutex &)=delete
RecMutex & | operator= (const RecMutex &)=delete
int | lock ()
int | trylock ()
int | unlock ()
| RecMutex ()
| ~RecMutex ()

## Static Public Member Functions

static int | test_support ()

## Detailed Description

A wrapper class for pthread mutexes which provides a recursive mutex.

See Also
  Thread::Thread Thread::RecMutex::Lock Thread::RecMutex::TrackLock Thread::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 be used interchangeably with those started by C++11 and with std::recursive_mutex and similar objects, as in C++11 on unix-like OSes these facilities will be built on top of pthreads (for which purpose C++11 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.

## Constructor & Destructor Documentation

Cgu::Thread::RecMutex::RecMutex | ( | const RecMutex & | ) |  |

delete

This class cannot be copied. The copy constructor is deleted.

Cgu::Thread::RecMutex::RecMutex | ( | ) |  |

Initialises the pthread mutex. It is not a cancellation point.

Exceptions

  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.)

Cgu::Thread::RecMutex::~RecMutex | ( | ) |  |

inline

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.

## Member Function Documentation

int Cgu::Thread::RecMutex::lock | ( | ) |  |

inline

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.

Returns
  0 if successful, otherwise the pthread mutex error number.

Note
  With this library implementation, the only pthread error number which could be returned by this method is EAGAIN, which it would do if the maximum recursive lock count for this mutex has been reached. Usually this number is at or around INT_MAX and hence the return value is usually not worth checking for except during debugging.

RecMutex& Cgu::Thread::RecMutex::operator= | ( | const RecMutex & | ) |  |

delete

This class cannot be copied. The assignment operator is deleted.

static int Cgu::Thread::RecMutex::test_support | ( | ) |  |

static

Indicates whether the system supports recursive mutexes. This method does not throw. It is thread safe.

Returns
  0 if the system supports recursive mutexes, -1 if it does not support recursive mutexes and 1 if pthread has run out of sufficient resources to test this at run time (in which case any attempt to create mutexes or start threads is likely to fail). Practically all recent linux and BSD distributions will support them, and this function would normally return 0. If it does not, it is still possible to use GStaticRecMutex objects (for which purpose see Cgu::Thread::GrecmutexLock).

Note
  The header file <c++-gtk-utils/cgu_config.h> defines the symbol CGU_HAVE_RECURSIVE_MUTEX if native recursive mutexes were found to be supported when this library was compiled. Programs using this library can therefore test for that symbol with the pre-processor for conditional compilation purposes (so that the program can, for example, be compiled to use GStaticRecMutex if that symbol is not defined). However, if the library was cross-compiled from one architecture to another, that symbol may not be defined even though the target architecture does, in fact, support them at program runtime. In other words, if CGU_HAVE_RECURSIVE_MUTEX is defined then this method will always return 0; but in the event of cross-compilation of the library this method (which provides a separate runtime test) might return 0, correctly showing support, even when CGU_HAVE_RECURSIVE_MUTEX is not defined.
  If glib >= 2.32 is installed, it can be assumed that native recursive mutexes are available, as glib >= 2.32 also uses them.

int Cgu::Thread::RecMutex::trylock | ( | ) |  |

inline

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.

Returns
  0 if successful, otherwise EBUSY or other pthread mutex error number.

Note
  With this library implementation, apart from EBUSY, the only other pthread error number which could be returned by this method is EAGAIN, which it would do if the maximum recursive lock count for this mutex has been reached. Usually this number is at or around INT_MAX and hence an EAGAIN error return value is usually not worth checking for except during debugging.

int Cgu::Thread::RecMutex::unlock | ( | ) |  |

inline

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.

Returns
  0 if successful, otherwise the pthread mutex error number.

Note
  With this library implementation, the only pthread error number which could be returned by this method is EPERM because the calling thread does not own the mutex (however POSIX does not require that return value in that case and hence the return value is usually not worth checking for except during debugging).

The documentation for this class was generated from the following file:

- mutex.h