Do pthread_mutex_lock and pthread_mutex_unlock functions call memory fence/barrier instructions? Or do the the lower level instructions like compare_and_swap implicity have memory barriers?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
Please take a look at section 4.11 of the POSIX specification.
Then a list of functions is given which synchronize memory, plus a few additional notes.
If that requires memory barrier instructions on some architecture, then those must be used.
About
compare_and_swap
: that isn't in POSIX; check the documentation for whatever you are using. For instance, IBM defines acompare_and_swap
function for AIX 5.3. which doesn't have full memory barrier semantics The documentation note says:From this documentation we can guess that IBM's
compare_and_swap
has release semantics: since the documentation does not require a barrier for the end of the critical section. The acquiring processor needs to issue an isync to make sure it is not reading stale data, but the publishing processor doesn't have to do anything.At the instruction level, some processors have compare and swap with certain synchronizing guarantees, and some don't.
They do, as well as thread creation.
Note, however, there are two types of memory barriers: compiler and hardware.
Compiler barriers only prevent the compiler from reordering reads and writes and speculating variable values, but don't prevent the CPU from reordering.
The hardware barriers prevent the CPU from reordering reads and writes. Full memory fence is usually the slowest instruction, most of the time you only need operations with acquire and release semantics (to implement spinlocks and mutexes).
With multi-threading you need both barriers most of the time.
Any function whose definition is not available in this translation unit (and is not intrinsic) is a compiler memory barrier.
pthread_mutex_lock
,pthread_mutex_unlock
,pthread_create
also issue a hardware memory barrier to prevent the CPU from reordering reads and writes.See C++ and Beyond 2012: Herb Sutter - atomic<> Weapons for more details.