Should ALL global variables be volatile-qualified?

后端 未结 5 1195
走了就别回头了
走了就别回头了 2020-12-31 09:48

In this example, does correctness require global_value to be declared volatile?

int global_value = 0;

void foo () {
    ++ global_v         


        
5条回答
  •  时光说笑
    2020-12-31 10:47

    The only uses of volatile involve longjmp, signal handlers, memory-mapped device drivers, and writing your own low-level multi-threaded synchronization primitives. For this last use however, volatile is not sufficient and may not even be necessary. You'll definitely also need asm (or compiler-specific or C1x atomics) for synchronization.

    volatile is not useful for any other purposes, including the code you asked about.

提交回复
热议问题