Alex Rivera | Logout

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

Asked 2009-07-07T04:08:10.747
46

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 normal reads?

Can volatile variables ever be placed in the cpu cache or is it always fetched from the main memory?

Edit
Report

1 Answer

3

It is architecture dependent. What volatile does is tell the compiler not to optimise that variable away. It forces most operations to treat the variable's state as an unknown. Because it is volatile, it could be changed by another thread or some other hardware operation. So, reads will need to re-read the variable and operations will be of the read-modify-write kind.

This kind of variable is used for device drivers and also for synchronisation with in-memory mutexes/semaphores.

answered 2009-07-07T05:37:46.150

Your Answer