Avoiding using recursive mutexes
The standard library provides several mutex types for protecting access to shared resources. std::recursive_mutex
and std::recursive_timed_mutex
are two implementations that allow you to use multiple locking in the same thread. A typical use for a recursive mutex is to protect access to a shared resource from a recursive function. An std::recursive_mutex
class may be locked multiple times from a thread, either with a call to lock()
or try_lock()
. When a thread locks an available recursive mutex, it acquires its ownership; as a result of this, consecutive attempts to lock the mutex from the same thread do not block the execution of the thread, creating a deadlock. The recursive mutex is, however, released only when an equal number of calls to unlock()
are made. Recursive mutexes may also have a greater overhead than non-recursive mutexes. For these reasons, when possible, they should be avoided. This recipe presents a use case for transforming a thread...