Search code examples
c++multithreadingc++11memory-modelstdatomic

atomic<T>.load() with std::memory_order_release


When writing C++11 code that uses the newly introduced thread-synchronization primitives to make use of the relaxed memory ordering, you usually see either

std::atomic<int> vv;
int i = vv.load(std::memory_order_acquire);

or

vv.store(42, std::memory_order_release);

It is clear to me why this makes sense.

My questions are: Do the combinations vv.store(42, std::memory_order_acquire) and vv.load(std::memory_order_release) also make sense? In which situation could one use them? What are the semantics of these combinations?


Solution

  • That's simply not allowed. The C++ (11) standard has requirements on what memory order constraints you can put on load/store operations.

    For load (§29.6.5):

    Requires: The order argument shall not be memory_order_release nor memory_order_acq_rel.

    For store:

    Requires: The order argument shall not be memory_order_consume, memory_order_acquire, nor memory_order_acq_rel.