首頁 > 後端開發 > C++ > 主體

為什麼要使用「memory_order_seq_cst」來設定透過「memory_order_relaxed」檢查的停止標誌?

Mary-Kate Olsen
發布: 2024-11-15 03:10:02
原創
957 人瀏覽過

Why Use `memory_order_seq_cst` for Setting a Stop Flag Checked with `memory_order_relaxed`?

Why Utilize memory_order_seq_cst for Setting Stop Flag if Checked with memory_order_relaxed?

In his "atomic<> weapons" presentation, Herb Sutter showcases atomic variable usage, including a scenario involving:

  • Main thread spawning worker threads
  • Workers checking a stop flag:

    while (!stop.load(std::memory_order_relaxed))
    {
      // Perform tasks
    }
    登入後複製
  • Main thread eventually sets stop to true using memory_order_seq_cst.

Sutter asserts that using memory_order_relaxed for checking the flag is acceptable due to minimal impact on thread stopping delay. However, the reason for employing memory_order_seq_cst for setting the stop flag remains unclear.

Analysis:

mo_relaxed is Sufficient for Both Loading and Storing Stop Flag:

There is no significant latency benefit in utilizing stronger memory orders, even if the latency of observing changes in stop or keep_running flags is crucial.

It is unclear why Sutter advises against relaxed store operations. However, the ISO C++ standard does not specify store visibility timing or factors affecting it. Implementations are only mandated to ensure visibility within a finite period.

Inter-Thread Latency and Implementation:

Inter-thread latency is primarily determined by the implementation. Real-world C++ implementations leverage hardware cache coherence mechanisms, typically resulting in low latency (tens of nanoseconds) for store visibility.

Neither seq_cst nor relaxed memory orders hasten store visibility; they merely control the behavior of subsequent operations relative to the store or load. Stronger orders do not accelerate events but delay other operations until the specified order is maintained.

Relaxed Visibility and Hardware Cache Coherency:

On real hardware with cache coherency, memory orders do not enhance store visibility timing; they solely manage the ability of subsequent operations to become globally visible before store commitment.

Benefits of Relaxed Memory Order for Stop Flag:

The primary benefits of relaxed memory order for checking the stop flag are:

  • Increased parallelism across loop iterations when the load result is false.
  • Avoidance of unnecessary instruction execution, especially on ISAs where acquire or seq_cst loads require additional instructions (e.g., ARMv7 dmb ish).

Conclusion:

In this scenario, memory_order_relaxed is appropriate for both loading and storing the stop flag. memory_order_seq_cst is not necessary for enhancing store visibility timing. Instead, it is used to enforce the desired ordering of subsequent operations and avoid issues with simultaneous writers.

以上是為什麼要使用「memory_order_seq_cst」來設定透過「memory_order_relaxed」檢查的停止標誌?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板