std::mutex vs std::recursive_mutex as class member

前端 未结 3 1120
粉色の甜心
粉色の甜心 2021-01-29 23:23

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 cl

3条回答
  •  Happy的楠姐
    2021-01-30 00:09

    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 guard(m);
            return data;
        }
        void increase_count() {
            std::lock_guard guard(m);
            increment_data();
        } 
        int increase_count_and_return() {
            std::lock_guard 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.

提交回复
热议问题