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.
Asked
Active
Viewed 581 times
3

curiousguy
- 8,038
- 2
- 40
- 58

Trismegistos
- 3,821
- 2
- 24
- 41
-
No. See also http://www.geeksforgeeks.org/mutex-vs-semaphore/ – yohjp Dec 03 '14 at 07:46
2 Answers
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.
6
The mutex must be locked by the current thread of execution, otherwise, the behavior is undefined.