Must I call atomic load/store explicitly?

前端 未结 2 1584
暖寄归人
暖寄归人 2020-12-25 09:51

C++11 introduced the std::atomic<> template library. The standard specifies the store() and load() operations to atomically set / get a varia

2条回答
  •  臣服心动
    2020-12-25 10:18

    You can do both, but the advantage of load()/store() is that they allow to specify memory order. It is important sometimes for performance, where you can specify std::memory_order_relaxed while atomic::operator T and atomic::operator= would use the most safe and slow std::memory_order_seq_cst. Sometimes it is important for correctness and readability of your code: although the default std::memory_order_seq_cst is most safe thus most likely to be correct, it is not immediately clear for the reader what kind of operation (acquire/release/consume) you are doing, or whether you are doing such operation at all (to answer: isn't relaxed order sufficient here?).

提交回复
热议问题