Do I have to use atomic for “exit” bool variable?

后端 未结 3 508
广开言路
广开言路 2020-12-01 09:09

I need to set a flag for another thread to exit. That other thread checks the exit flag from time to time. Do I have to use atomic for the flag or just a plain bool is enoug

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 09:55

    actually, nothing goes wrong with plain bool in this particular example. the only notice is to declare bool exit variable as volatile to keep it in memory. both CISC and RISC architectures implement bool read/write as strictly atomic processor instruction. also modern multocore processors have advanced smart cache implementstion. so, any memory barriers are not necessary. the Standard citation is not appropriate for this particular case because it deals with the only one writing and the reading from the only one thread.

提交回复
热议问题