Why there is no scoped locks for multiple mutexes in C++0x or Boost.Thread?
- by Vicente Botet Escriba
C++0x thread library or Boost.thread define non-member variadic template function that lock all lock avoiding dead lock.
template <class L1, class L2, class... L3> void lock(L1&, L2&, L3&...);
While this function avoid help to deadlock, the standard do not includes the associated scoped lock to write exception safe code.
{
std::lock(l1,l2);
// do some thing
// unlock li l2 exception safe
}
That means that we need to use other mechanism as try-catch block to make exception safe code or define our own scoped lock on multiple mutexes ourselves or even do that
{
std::lock(l1,l2);
std::unique_lock lk1(l1, std::adopted);
std::unique_lock lk2(l2, std::adopted);
// do some thing
// unlock li l2 on destruction of lk1 lk2
}
Why the standard doesn't includes a scoped lock on multiple mutexes of the same type, as for example
{
std::array_unique_lock<std::mutex> lk(l1,l2);
// do some thing
// unlock l1 l2 on destruction of lk
}
or tuples of mutexes
{
std::tuple_unique_lock<std::mutex, std::recursive_mutex> lk(l1,l2);
// do some thing
// unlock l1 l2 on destruction of lk
}
Is there something wrong on the design?