我想了解如何在C++中工作內存障礙。 例如,我使用的std ::原子在這種情況下:瞭解std ::原子內存障礙
#include <iostream>
#include <atomic>
int main()
{
std::atomic<int> a;
int n = load();//returns 1 or other value written by other thread
a.store (n, std::memory_order_release);
}
是上述語義的代碼等於下面的代碼?
#include <iostream>
#include <atomic>
int main()
{
std::atomic<int> a;
int n = load();
std::atomic_thread_fence(std::memory_order_release);
n = 100;//assume assignment is atomic
}
如果我是正確的,我可以肯定的是行爲是相等的,可以接受記憶障礙,參數中的所有C++函數?
你的意思是在主函數的第二行,在兩個例子'int n = a.load(std :: memory_order_acquire)'和第二個例子中,第四行:'a = 100'? – Oliv
不,加載函數不是std :: atomic加載函數。這是一個用戶實現的函數,它返回一些數據 –
這個例子看起來很混亂。第一部分清楚地將一個值存儲到'a'中。但是第二部分片段根本沒有提及'a',爲什麼它在那裏? –