Why does a std::atomic store with sequential consistency use XCHG?

mov-store + mfence and xchg are both valid ways to implement a sequential-consistency store on x86. The implicit lock prefix on an xchg with memory makes it a full memory barrier, like all atomic RMW operations on x86. (x86’s memory-ordering rules essentially make that full-barrier effect the only option for any atomic RMW: it’s both … Read more

Reproduce torn reads of decimal in C#

This code will demonstrate a torn read of a Decimal: using System; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { void run() { Task.Run((Action) setter); Task.Run((Action) checker); Console.WriteLine(“Press <ENTER> to stop”); Console.ReadLine(); } void setter() { while (true) { d = VALUE1; d = VALUE2; } } void checker() { for (int count = 0;; … Read more

Reading an int that’s updated by Interlocked on other threads

I’m a firm believer in that if you’re using interlocked to increment shared data, then you should use interlocked everywhere you access that shared data. Likewise, if you use insert you favorite synchronization primitive here to increment shared data, then you should use insert you favorite synchronization primitive here everywhere you access that shared data. … Read more

Is there any compiler barrier which is equal to asm(“” ::: “memory”) in C++11?

re: your edit: But I do not want to use atomic variable. Why not? If it’s for performance reasons, use them with memory_order_relaxed and atomic_signal_fence(mo_whatever) to block compiler reordering without any runtime overhead other than the compiler barrier potentially blocking some compile-time optimizations, depending on the surrounding code. If it’s for some other reason, then … Read more

Is a memory barrier required to read a value that is atomically modified?

No, you don’t need barriers, but your code is broken anyway if readers and writers call these functions in different threads. Especially if a reader calls the read function in a loop. TL:DR: use C++11 std::atomic<long> m_value with return m_value++ in the increment and return m_value in the reader. That will give you sequential consistency … Read more

Should Interlocked.CompareExchange also a volatile variable?

No, volatile wouldn’t be helpful at all, and certainly not for this reason. It would just give that first read “acquire” semantics instead of effectively relaxed, but either way will compile to similar asm that runs a load instruction. if you get a dirty value from a CPU cache CPU caches are coherent, so anything … Read more