Consider this C++ statement:
foo.exchange(bar, std::memory_order_acq_rel);
Can the above statement is exactly equivalent to any of the below?
1)
foo.exchange(bar, std::memory_order_acquire);
dummy.store(0, std::memory_order_release);
dummy.store(0, std::memory_order_release);
foo.exchange(bar, std::memory_order_acquire);
foo.exchange(bar, std::memory_order_release);
dummy.load(std::memory_order_acquire);
dummy.load(std::memory_order_acquire);
foo.exchange(bar, std::memory_order_release);
In case they are not equivalent, please mention why they are not.
The operations are completely different for a simple reason. Release operation on variable
ais not equivalent in any way to release operation on variableb. To synchronize with the thread one would need to call acquire on variablebrather thana. That's the difference. Yes, the memory instruction are tied to variables.So replacing
acq_relwith lesser instruction onfooand an instruction ondummywill not properly synchornize with threads that call eitheracquireorreleaseonfoodepending on what instruction was called onfoo.Albeit if you called a discarded
loadonfooin addition to theexchangewith the complememting instruction, the effect would be pretty much equivalent. Also you could call a general fence that would trigger a stronger synchronization instruction.