Is the explanation of relaxed ordering erroneous in cppreference?

后端 未结 3 1351
粉色の甜心
粉色の甜心 2021-02-07 15:03

In the documentation of std::memory_order on cppreference.com there is an example of relaxed ordering:

Relaxed ordering

Atomic operations tagged

3条回答
  •  说谎
    说谎 (楼主)
    2021-02-07 16:09

    If there are two statements, the compiler will generate code in sequential order so code for the first one will be placed prior to the second one. But cpus internally have pipelines and are able to run assembly operations in parallel. Statement C is a load instruction. While memory is being fetched the pipeline will process the next few instructions and given they are not dependent on the load instruction they could end up being executed prior to C being finished (e.g. data for D was in cache, C in main memory).

    If the user really needed the two statements to be executed sequentially, stricter memory ordering operations can be used. In general users don't care as long as the program is logically correct.

提交回复
热议问题