2013-08-06 34 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)。值得注意的是而不是表示只能有一個這樣的同步負載;所以確實任何從原子存儲版本獲取其值的原子加載獲取與該存儲同步。

相關問題