You can consider the storage / loading functions with the order of freeing / receiving memory in the form of the following pseudo-code:
template<class T> struct weak_atomic { void store(T newValue) { ReleaseBarrier(); m_value = newValue; } T load() { T value = m_value; AcquireBarrier(); return value; } volatile T m_value; }
you said
Storage Fence created by x guarantee empty storage buffer
As I understand it, the release memory barrier will cause the CPU to start its storage buffer, but will execute before , applying the new value to x. Thus, it is possible to read the old value from x by another processor.
In any case, weak atomism is a very complex field. Before you start programming with locking, make sure that you understand the memory barriers.
ADDED
You still seem to be confused with memory barriers. This is a fairly common example of their use.
volatile int x; volatile bool ok; void thread_1() { x = 100; ok = true; } void thread_2() { if (ok) { assert(x == 100); } }
Due to non-standard execution, you can get the following sequence:
thread 1 sets ok to true thread 2 checks ok is true and reads some garbage from x thread 1 sets x to 100 but it is too late
Another possible sequence:
thread 2 reads some garbage from x thread 2 checks for ok value
We can fix this with liberation and acquire memory barriers.
volatile int x; volatile bool ok; void thread_1() { x = 100; ReleaseBarrier(); ok = true; } void thread_2() { if (ok) { AcquireBarrier(); assert(x == 100); } }
ReleaseBarrier() ensures that writing to memory cannot jump over the barrier. This means that ok set only to true when x already contains a valid value.
AcquireBarrier() ensures that memory reading cannot jump over the barrier. This means that the value of x is only read after checking the status ok .
This means that you are supposed to use a release / receive pair. We can rewrite this example with my weak_atomic .
volatile int x; weak_atomic<bool> ok; void thread_1() { x = 100; ok.store(true); } void thread_2() { if (ok.load()) { assert(x == 100); } }