3

Can I lock std::mutex in one thread and unlock it in other thread? This is theoretical question without any specific engineering context. I am just curious.

curiousguy
  • 8,038
  • 2
  • 40
  • 58
Trismegistos
  • 3,821
  • 2
  • 24
  • 41

2 Answers2

7

C++ standard, [thread.mutex.requirements.mutex]/21 and 22:

The expression m.unlock() shall be well-formed and have the following semantics:

      Requires: The calling thread shall own the mutex.

A violation of requirements in a Requires clause induces undefined behavior.

Community
  • 1
  • 1
Columbo
  • 60,038
  • 8
  • 155
  • 203
6

cppreference reads:

The mutex must be locked by the current thread of execution, otherwise, the behavior is undefined.