2013-08-06 35 views
3

我想知道是否有2个线程正在使用memory_order_acquire进行加载,并且一个线程正在使用memory_acquire_release进行存储,只会将加载与两个线程之一做负载?这意味着它只能与存储/加载的其中一个线程同步,或者您可以使用多个线程执行同步加载,而只需要一个线程进行存储。这似乎是在研究它是一对一关系之后的情况,但读 - 修改 - 写操作似乎是连锁的,见下文。多线程可以使用memory_order_acquire使用std :: atomic同步一个加载可以使用std :: atomic

看来,如果线程正在做一个工作的精细读 - 修改 - 写操作一样fetch_sub的话,好像这些都将有一个链接的版本即使没有同步,与reader1_thread之间的关系,并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)); 
} 

回答

3

该标准说“一个原子存储释放与一个从存储中获取其值的加载获取同步”(C++ 11§1.10[intro.multithread] p8)。值得注意的是而不是表示只能有一个这样的同步负载;所以确实任何从原子存储版本获取其值的原子加载获取与该存储同步。

相关问题