Trismegistos
Trismegistos

Reputation: 3882

Thread safety of std::mutex

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.

Upvotes: 3

Views: 598

Answers (2)

Columbo
Columbo

Reputation: 60979

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.

Upvotes: 7

user1804599
user1804599

Reputation:

cppreference reads:

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

Upvotes: 6

Related Questions