2 comments

[ 3.7 ms ] story [ 18.6 ms ] thread
It is not a good idea to use <pre>volatile</pre> for creating multi-threaded applications like the author is explaining it will just end up creating more problems than solving one. Better use a library.

The problem with volatile is that if mark everything volatile you program is just going to run a lot slower. If you don't make all of them volatile it is still possible the hardware could reorder references even if the compiler doesn't.

The hardware will reorder references regardless of how many of them are marked volatile - CPU doesn't know about volatile directives. The other related problem is caching - reading the same address from different CPUs will occasionally yield different results and may produce inconsistent results. Like so:

  a=b=c=1;a=2;b=2;c=2;
Reading from a different CPU may produce any combination of values 1 and 2, including the seemingly impossible 2,1,2.

Just use a library.