What is meant by using an explicit memory fence?
相关问题
- Why it isn't advised to call the release() met
- Managing data-store concurrency as microservices s
- Custom TaskScheduler, SynchronizationContext?
- Prevent Concurrency In Nodejs
- coding a proof for potential concurrency issue
相关文章
- Is there an existing solution for these particular
- How does the piggybacking of current thread variab
- Implement a mutex in Java using atomic variables
-
tbb:concurrent_hash_map
: sample code for Intel - Is there an implementation of std::async which use
- Multi Thread Java, but only one thread working
- Producer/Consumer - producer adds data to collecti
- CompletionStage chaining when 2nd stage is not int
Copying my answer to another question, What are some tricks that a processor does to optimize code?:
In my experience it refers to a memory barrier, which is an instruction (explicit or implicit) to synchronize memory access between multiple threads.
The problem occurs in the combination of modern agressive compilers (they have amazing freedom to reorder instructions, but usually know nothing of your threads) and modern multicore CPUs.
A good introduction to the problem is the "The 'Double-Checked Locking is Broken' Declaration". For many, it was the wake-up call that there be dragons.
Implicit full memory barriers are usually included in platform thread synchronization routines, which cover the core of it. However, for lock-free programming and implementing custom, lightweight synchronization patterns, you often need just the barrier, or even a one-way barrier only.
For performance gains modern CPUs often execute instructions out of order to make maximum use of the available silicon (including memory read/writes). Because the hardware enforces instructions integrity you never notice this in a single thread of execution. However for multiple threads or environments with volatile memory (memory mapped I/O for example) this can lead to unpredictable behavior.
A memory fence/barrier is a class of instructions that mean memory read/writes occur in the order you expect. For example a 'full fence' means all read/writes before the fence are comitted before those after the fence.
Note memory fences are a hardware concept. In higher level languages we are used to dealing with mutexes and semaphores - these may well be implemented using memory fences at the low level and explicit use of memory barriers are not necessary. Use of memory barriers requires a careful study of the hardware architecture and more commonly found in device drivers than application code.
The CPU reordering is different from compiler optimisations - although the artefacts can be similar. You need to take separate measures to stop the compiler reordering your instructions if that may cause undesirable behaviour (e.g. use of the volatile keyword in C).
Wikipedia knows all...