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 of a recursive mutex is to protect access to a shared resource from a recursive function. Recursive mutexes have a greater overhead than non-recursive mutexes and, when possible, they should be avoided. This recipe presents a use case for transforming a thread-safe type using a recursive mutex into a thread-safe type using a non-recursive mutex.
Getting ready
You need to be familiar with the various mutexes and locks available in the standard library. I recommend that you read the previous recipe, Synchronizing access to shared data with mutex and locks, to get an overview of them.
The purpose of this recipe is to transform the following class so we can avoid using std::recursive_mutex
:
class foo_rec { std:...