I have seen some people hate on recursive_mutex
:
http://www.zaval.org/resources/library/butenhof1.html
But when thinking about how to implement a class that is thread safe (mutex protected), it seems to me excruciatingly hard to prove that every method that should be mutex protected is mutex protected and that mutex is locked at most once.
So for object oriented design, should std::recursive_mutex
be default and std::mutex
considered as an performance optimization in general case unless it is used only in one place (to protect only one resource)?
To make things clear, I'm talking about one private nonstatic mutex. So each class instance has only one mutex.
At the beginning of each public method:
{ std::scoped_lock<std::recursive_mutex> sl;
If you want to be able to call public methods from different threads inside other public methods of a class and many of these public methods change the state of the object, you should use a recursive mutex.
std::recursive_mutex The recursive_mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads.
Because the recursive mutex has a sense of ownership, the thread that grabs the mutex must be the same thread that releases the mutex. In the case of non-recursive mutexes, there is no sense of ownership and any thread can usually release the mutex no matter which thread originally took the mutex.
A lock_guard always holds a lock from its construction to its destruction. A unique_lock can be created without immediately locking, can unlock at any point in its existence, and can transfer ownership of the lock from one instance to another.
Most of the time, if you think you need a recursive mutex then your design is wrong, so it definitely should not be the default.
For a class with a single mutex protecting the data members, then the mutex should be locked in all the public
member functions, and all the private
member functions should assume the mutex is already locked.
If a public
member function needs to call another public
member function, then split the second one in two: a private
implementation function that does the work, and a public
member function that just locks the mutex and calls the private
one. The first member function can then also call the implementation function without having to worry about recursive locking.
e.g.
class X { std::mutex m; int data; int const max=50; void increment_data() { if (data >= max) throw std::runtime_error("too big"); ++data; } public: X():data(0){} int fetch_count() { std::lock_guard<std::mutex> guard(m); return data; } void increase_count() { std::lock_guard<std::mutex> guard(m); increment_data(); } int increase_count_and_return() { std::lock_guard<std::mutex> guard(m); increment_data(); return data; } };
This is of course a trivial contrived example, but the increment_data
function is shared between two public member functions, each of which locks the mutex. In single-threaded code, it could be inlined into increase_count
, and increase_count_and_return
could call that, but we can't do that in multithreaded code.
This is just an application of good design principles: the public member functions take responsibility for locking the mutex, and delegate the responsibility for doing the work to the private member function.
This has the benefit that the public
member functions only have to deal with being called when the class is in a consistent state: the mutex is unlocked, and once it is locked then all invariants hold. If you call public
member functions from each other then they have to handle the case that the mutex is already locked, and that the invariants don't necessarily hold.
It also means that things like condition variable waits will work: if you pass a lock on a recursive mutex to a condition variable then (a) you need to use std::condition_variable_any
because std::condition_variable
won't work, and (b) only one level of lock is released, so you may still hold the lock, and thus deadlock because the thread that would trigger the predicate and do the notify cannot acquire the lock.
I struggle to think of a scenario where a recursive mutex is required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With