Are mutex lock functions sufficient without volatile?

Simplest answer is volatile is not needed for multi-threading at all. The long answer is that sequence points like critical sections are platform dependent as is whatever threading solution you’re using so most of your thread safety is also platform dependent. C++0x has a concept of threads and thread safety but the current standard does … Read more

Does it make any sense to use the LFENCE instruction on x86/x86_64 processors?

Bottom line (TL;DR): LFENCE alone indeed seems useless for memory ordering, however it does not make SFENCE a substitute for MFENCE. The “arithmetic” logic in the question is not applicable. Here is an excerpt from Intel’s Software Developers Manual, volume 3, section 8.2.2 (the edition 325384-052US of September 2014), the same that I used in … Read more

How do I Understand Read Memory Barriers and Volatile

There are read barriers and write barriers; acquire barriers and release barriers. And more (io vs memory, etc). The barriers are not there to control “latest” value or “freshness” of the values. They are there to control the relative ordering of memory accesses. Write barriers control the order of writes. Because writes to memory are … Read more

Is function call an effective memory barrier for modern platforms?

Memory barriers aren’t just to prevent instruction reordering. Even if instructions aren’t reordered it can still cause problems with cache coherence. As for the reordering – it depends on your compiler and settings. ICC is particularly agressive with reordering. MSVC w/ whole program optimization can be, too. If your shared data variable is declared as … Read more