C++ atomic CAS(compare-and-swap) operation does not change value

*爱你&永不变心* 提交于 2021-02-17 03:44:34

问题


In the following example, what actually happens? Why value does not changes after successful exchange?

Live: https://wandbox.org/permlink/f5VYSKfQ9UJqa8FQ

std::atomic<bool> flag{false};

int main()
{
    std::thread thread([](){
        while(true){        
            // wait until flag not becomes true
            {
              bool expect = true;
              while(!flag.compare_exchange_strong(expect, false, std::memory_order_acq_rel)){
                  std::cout << "wait" << std::endl;
              }
            }
            
            std::cout << "work" << std::endl;
        }
    });
    flag.store(true, std::memory_order_release);
    thread.join();
}

Output:

work
wait
work
wait
...

回答1:


Consider what happens with:

          bool expect = true;
          while(!flag.compare_exchange_strong(expect, false, std::memory_order_acq_rel)){
              std::cout << "wait" << std::endl;
          }

when the flag is false. The first time the test in the while loop runs, expect will be true, so does not match the flag. So expect is updated to the false, and the function returns false. So wait is printed and the loop repeats. The second test in the loop, expect will now be false, which matches flag, so flag will be set to false (a noop as it already is), and the loop will exit.

The net effect will be to always set flag to false, and print wait if it was already false. Thus, the output you see.



来源:https://stackoverflow.com/questions/62863102/c-atomic-cascompare-and-swap-operation-does-not-change-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!