Reputation:
I see C++11 mutexes lock is not void lock() volatile
. How does the compiler know which functions are memory barriers and which are not? Are all functions barriers even if they are not volatile? What are some less known memory barriers and memory barriers everyone should know?
Upvotes: 10
Views: 907
Reputation: 477040
The actual implementation of your std::mutex
will be such that the compiler doesn't perform illegal reordering, doesn't elide variable loads, and it will ensure that the lock variable is accessed atomically and that the CPU performs the necessary memory barriers for lock acquisition and release.
The details of how much work needs to be done to ensure this vary from platform to platform, but your library implementation will Do The Right Thing.
Upvotes: 5
Reputation: 92261
The runtime library has to implement a mutex in a way so that the compiler knows! The language standard doesn't say anything about how to do this.
Likely, it involves a call to some operating system service that works as a memory barrier. Or the compiler can have an extension, like void _ReadWriteBarrier();
Upvotes: 5