lock

lock can be used to lock multiple mutexes simultaneously. Resource deadlock is avoided if more than one mutex is locked at the same time.

The order in which lock calls mutex. lock() and mutex. unlock() is not specified but, if an exception occurs due to either function, then all mutexes are unlocked before the exception is rethrown.

#include <mutex>

::std::mutex
   mutex0,
   mutex1;

::std::timed_mutex
   mutex2;

int main(int argc, char ** argv)
{
   //
   // Mutexes do not need to be of the same type:
   //
   
   ::std::lock( mutex0, mutex1, mutex2 );
   
   mutex0.unlock();
   mutex1.unlock();
   mutex2.unlock();
   
   return 0;
}