Why volatile and MemoryBarrier do not prevent operations reordering?

后端 未结 4 1246
耶瑟儿~
耶瑟儿~ 2021-02-05 17:10

If I understand meaning of volatile and MemoryBarrier correctly than the program below has never to be able to show any result.

It catches reordering of write operations

4条回答
  •  情歌与酒
    2021-02-05 17:38

    You aren't cleaning the variables between tests, so (for all but the first) initially a is 2 and b is 20 - before Write has done anything.

    Check can get that initial value of a (so tempA is 2), and then Write can get in, get as far as changing b to 10.

    Now Check reads the b (so tempB is 10).

    Et voila. No re-order necessary to repro.

    Reset a and b to 0 between runs and I expect it will go away.

    edit: confirmed; "as is" I get the issue almost immediately (<2000 iterations); but by adding:

    while (continueTrying)
    {
        a = b = 0; // reset <======= added this
    

    it then loops for any amount of time without any issue.

    Or as a flow:

    Write                   A=  B=        Check
    
    (except first run)      2   20
                                          int tempA = a;
    a = 1;                  1   20
    Thread.MemoryBarrier();
    b = 10;                 1   10
                                          int tempB = b;
    

提交回复
热议问题