You can use std::lock
(or equivalently boost::lock
, collectively "the lock functions") to perform shared ownership locking as well as exclusive locking, depending on what you pass in to be locked - anything Lockable
will work. For example, if you have want to lock two std::mutex
/boost::mutex
s A, B
and boost::shared_mutex SM
in exclusive mode, you simply pass the three to std::lock
:
std::lock(A, B, SM);
if you instead want to lock SM
in shared ownership mode, you can create an unlocked boost::shared_lock
for it and pass that into the locking functions:
boost::shared_lock<boost::shared_mutex> bsl{SM, boost::defer_lock};
std::lock(A, B, bsl);
On a note unrelated to your question - style preference - I prefer to always construct RAII locks to pass into std::lock
so I can't screw up unlocking, so I'd actually write:
auto alk = boost::make_unique_lock(A, std::defer_lock);
auto blk = boost::make_unique_lock(B, std::defer_lock);
// boost::make_shared_lock(), where are you?!?
boost::shared_lock<boost::shared_mutex> bsl{SM, boost::defer_lock};
std::lock(alk, blk, bsl);