Are volatile variable 'reads' as fast as normal reads?

前端 未结 5 2016
攒了一身酷
攒了一身酷 2020-11-30 05:35

I know that writing to a volatile variable flushes it from the memory of all the cpus, however I want to know if reads to a volatile variable are as fast as nor

5条回答
  •  一个人的身影
    2020-11-30 05:58

    Volatile reads cannot be as quick, especially on multi-core CPUs (but also only single-core). The executing core has to fetch from the actual memory address to make sure it gets the current value - the variable indeed cannot be cached.

    As opposed to one other answer here, volatile variables are not used just for device drivers! They are sometimes essential for writing high performance multi-threaded code!

提交回复
热议问题