Can non-atomic-load be reordered after atomic-acquire-load?

后端 未结 4 1053
灰色年华
灰色年华 2021-01-02 22:28

As known in since C++11 there are 6 memory orders, and in documentation written about std::memory_order_acquire:

  • http://en.cppreference.com/w/cpp/
4条回答
  •  时光取名叫无心
    2021-01-02 23:18

    The reference you cited is pretty clear: you can't move reads before this load. In your example:

    static std::atomic X;
    static int L;
    
    
    void thread_func() 
    {
        int local1 = L;  // (1)
        int x_local = X.load(std::memory_order_acquire);  // (2)
        int local2 = L;  // (3)
    }
    

    memory_order_acquire means that (3) cannot happen before (2) (the load in (2) is sequenced before thr load in (3)). It says nothing about the relationship between (1) and (2).

提交回复
热议问题