How do memory_order_seq_cst and memory_order_acq_rel differ?

前端 未结 4 1224
夕颜
夕颜 2020-12-02 14:02

Stores are release operations and loads are acquire operations for both. I know that memory_order_seq_cst is meant to impose an additional total ordering for al

4条回答
  •  爱一瞬间的悲伤
    2020-12-02 14:49

    Try to build Dekkers or Petersons algorithm with just acquire/release semantics.

    That won't work because acquire/release semantics doesn't provide [StoreLoad] fence.

    In case of Dekkers algorithm:

    flag[self]=1 <-- STORE
    while(true){
        if(flag[other]==0) { <--- LOAD
            break;
        }
        flag[self]=0;
        while(turn==other);
        flag[self]=1        
    }
    

    Without [StoreLoad] fence the store could jump in front of the load and then the algorithm would break. 2 threads at the same time would see that the other lock is free, set their own lock and continue. And now you have 2 threads within the critical section.

提交回复
热议问题