C++11 memory_order_acquire and memory_order_release semantics?

The spinlock mutex implementation looks okay to me. I think they got the definitions of acquire and release completely wrong. Here is the clearest explanation of acquire/release consistency models that I am aware of: Gharachorloo; Lenoski; Laudon; Gibbons; Gupta; Hennessy: Memory consistency and event ordering in scalable shared-memory multiprocessors, Int’l Symp Comp Arch, ISCA(17):15-26, 1990, … Read more

Double-Checked Lock Singleton in C++11

I think this a great question and John Calsbeek has the correct answer. However, just to be clear a lazy singleton is best implemented using the classic Meyers singleton. It has garanteed correct semantics in C++11. ยง 6.7.4 … If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall … Read more

How to perform atomic operations on Linux that work on x86, arm, GCC and icc?

Projects are using this: http://packages.debian.org/source/sid/libatomic-ops If you want simple operations such as CAS, can’t you just just use the arch-specific implementations out of the kernel, and do arch checks in user-space with autotools/cmake? As far as licensing goes, although the kernel is GPL, I think it’s arguable that the inline assembly for these operations is … Read more

Are C/C++ fundamental types atomic?

No, fundamental data types (e.g., int, double) are not atomic, see std::atomic. Instead you can use std::atomic<int> or std::atomic<double>. Note: std::atomic was introduced with C++11 and my understanding is that prior to C++11, the C++ standard didn’t recognize the existence of multithreading at all. As pointed out by @Josh, std::atomic_flag is an atomic boolean type. … Read more

Which is more efficient, basic mutex lock or atomic integer?

Atomic operations leverage processor support (compare and swap instructions) and don’t use locks at all, whereas locks are more OS-dependent and perform differently on, for example, Win and Linux. Locks actually suspend thread execution, freeing up cpu resources for other tasks, but incurring in obvious context-switching overhead when stopping/restarting the thread. On the contrary, threads … Read more