Keyword Volatile
Usually the compiler will put the variable in the cache for fast access, Volatile make the compiler not do optimization on variable, so the volatile variable is on the main memory.
In the multithreading, if the variable is executed in atomic read (not read modify write), you can state the variable as volatile, multi threads can get the only copy of the variable from main memory. So if you do not have mutex and lock, just use volatile to do synchronization, although it is spinlock…
Basically, Volatile can only replace mutex
http://www.ddj.com/cpp/184403766
http://blogs.sun.com/sin/entry/why_to_use_volatile_in1
Jan 18, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment