What is the underlying mechanism for the relaxed memory model in c++ 11?

Say there are two threads, t1 and t2. t1 modifies a global flag f1 and t2 modifies a global flag f2 at around the same time. Following this if t1 tries to read f2 (or t2 reads f1) could it ever read the older value of f2 (or f1)?

This question really isn't about C++ 11 at all. I just want to understand the behaviour of a multi-threaded program with respect to cache coherency and how and why the relaxed model arises and is useful.


Relaxed ordering guarantees only that a thread will never observe an older value of a particular memory location than the newest value that it has observed. If t1 has never observed f2 before the particular read that you mention in the question, it can observe any value that was ever written into f2.

On its own, using memory_order_relaxed with a C++11 std::atomic is quite weak. It simply guarantees that the read/write is indivisible with respect to other threads: no thread will ever see a partially written (torn) value. Relaxed orderings are really only useful when they interact with other memory operations, or for read-modify-write actions. Read-modify-write actions are guaranteed to read the most recently written value regardless of memory ordering.

Need Your Help

Creating temporary files in bash

linux bash unix

Are there objectively better ways to create temporary files in bash scripts?

Debugging Issues (Android Eclipse)

android eclipse debugging android-4.2-jelly-bean
