![](/img/trans.png)
[英]std::memory_order_relaxed atomicity with respect to the same atomic variable
[英]Is it safe to use memory_order_relaxed to load an atomic variable, when in the only thread that writes to that variable?
我有一個寫入原子變量的線程。 (使用memory_order_release)。 線程A
還有許多其他線程從該變量讀取。 (使用memory_order_acquire)。 線程B。
在線程A中使用memory_order_relaxed讀取該變量是否安全?
是的,從某種意義上講,它的行為與單線程操作的非原子變量相同,這是安全的。
例如,如果寫釋放在程序順序輕松讀取之前測序 ,並沒有其他寫入該變量,然后輕松讀取將看到由寫入釋放寫入值( N3337 §1.10.12-13 )。
是的,當線程A中的讀取變量時,使用memory_order_relaxed
就足夠了。由於程序順序,此讀取將始終與發生在寫之前 ,發生在同一線程中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.