Search code examples
c++11atomic

Can multiple threads synchronize a load with a single store using memory_order_acquire using std::atomic


I was wondering if you have 2 threads that are doing a load using memory_order_acquire and one thread doing a store using memory_acquire_release will the load only be synched with one of the two threads doing the load? Meaning is it only able to synch up with one of the threads for the store / load or can you have multiple threads doing a synchronized load with a single thread doing a store. This seems to be the case after researching it is a one to one relationship but read-modify-write operations seems to chain, see below.

It seems to work fine if the threads are doing a read-modify-write operation like fetch_sub then it seems these will have a chained release even though there is no synchronize-with relationship between reader1_thread and reader2_thread

std::atomic<int> s;

void loader_thread()
{
  s.store(1,std::memory_order_release);
}

// seems to chain properly if these were fetch_sub instead of load, 
// wondering if just loads will be synchronized as well meaning both threads
// will be synched up with the store in the loader thread or just the first one

void reader1_thread()
{
 while(!s.load(std::memory_order_acquire));
}

void reader2_thread()
{
 while(!s.load(std::memory_order_acquire));
}

Solution

  • The standard says that "an atomic store-release synchronizes with a load-acquire that takes its value from the store" (C++11 §1.10 [intro.multithread] p8). It notably does not say that there can only be one such synchronizing load; so indeed any atomic load-acquire that takes its value from an atomic store-release synchronizes with that store.