What is the difference between a static global and a static volatile variable?

后端 未结 7 2026
情歌与酒
情歌与酒 2020-12-08 04:58

I have used a static global variable and a static volatile variable in file scope,

both are updated by an ISR and a main loop and main loop checks the value of the varia

7条回答
  •  萌比男神i
    2020-12-08 05:13

    They may not be in different in your current environment, but subtle changes could affect the behavior.

    • Different hardware (more processors, different memory architecture)
    • A new version of the compiler with better optimization.
    • Random variation in timing between threads. A problem may only occur one time in 10 million.
    • Different compiler optimization settings.

    It is much safer in the long run to use proper multithreading constructs from the beginning, even if things seem to work for now without them.

    Of course, if your program is not multi-threaded then it doesn't matter.

提交回复
热议问题