[英]In C++, is there any effective difference between a acquire/release atomic access and a relaxed access combined with a fence?
[英]c++ release fence with atomic acquire operation example
我正在尋找有關釋放圍欄和原子獲取操作的同步示例。 我發現的所有發布圍欄示例都是圍欄-圍欄同步。
這很簡單。你可以像這樣一起使用釋放柵欄和獲取操作:
int main()
{
int x = 0;
std::atomic<bool> flag{false};
std::thread t1{[&]
{
x = 42;
std::atomic_thread_fence(std::memory_order_release);
flag.store(true, std::memory_order_relaxed);
}};
std::thread t2{[&]
{
if (flag.load(std::memory_order_acquire))
{
assert(x == 42); // cannot fire
}
}};
t1.join(); t2.join();
}
請注意,放松存儲必須在釋放柵欄之后進行排序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.