seccpur seccpur - 5 months ago 36
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)


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