seccpur seccpur - 1 year ago 89
C++ Question

Will two overlapping mutexes lead to deadlock?

Consider the following member functions. Will it lead to a deadlock?

void foo::insert(item_ptr item)
lock_guard<mutex> lock(mu_);

void foo::insert(vector<item_ptr> items)
lock_guard<mutex> lock(mu_); // will this lead
// to deadlock?
for(auto item:items)

Answer Source

If mu_ is a std::mutex, then you have a problem. A std::mutex does not support recursive locks and cannot be locked twice by the same thread. See:

IF mu_ is a std::recursive_mutex, then you have no problem. A thread can lock a recursive mutex if it already has it locked. See

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download