Locking multiple mutexes

后端 未结 3 1062
猫巷女王i
猫巷女王i 2020-12-16 12:31

I\'m wondering if it\'s possible to lock multiple mutexes at the same time, like:

 Mutex1.Lock();
 {
     Mutex2.Lock();
     {
          // Code locked by m         


        
3条回答
  •  温柔的废话
    2020-12-16 12:41

    std::lock seems to exist for this purpose.

    Locks the given Lockable objects lock1, lock2, ..., lockn using a deadlock avoidance algorithm to avoid deadlock. The objects are locked by an unspecified series of calls to lock, try_lock, unlock. If a call to lock or unlock results in an exception, unlock is called for any locked objects before rethrowing.

    http://en.cppreference.com/w/cpp/thread/lock

提交回复
热议问题