I will repeat, you CAN do without volatile. You CAN do without a lawn mower. Both will cause extra work/overhead, however. But used correctly, volatile GREATLY improves efficiency, eliminates the need for unnecessary locking and unlocking, and the resulting cache thrashing. Period. And it works perfectly, done correctly. You have your head stuck so far up the standards bunghole you can't see any other way to do things more efficiently. I am not bound by such a small box. Volatile is part of the C standard. It works quite well.syzygy wrote:Do you understand "using the synchronisation primitives of a thread library, e.g. pthreads or C++11 threads"?bob wrote:No. If lock is based on ANY external procedure call, the compiler will reload any global variables referenced prior to the call. If no lock is called, the compiler will reload ANY global variable referenced prior to the call, UNLESS the compiler has access to the source code for the procedures being called so that it can directly determine which global variables (if any) might be modified.syzygy wrote:Look at my two (identical) statements.syzygy wrote:For multithreaded programs using volatile is not necessary if you are properly using the synchronisation primitives of a thread library, e.g. pthreads or C++11 threads.They are correct.syzygy wrote:If "lock" is based on phtreads primitives or a similar library, which is the case I am talking about, then "lock" acts as a memory barrier. The compiler will reload "variable". No need for making it volatile.
Agreed?
POSIX guarantees that it works. Just treat it as a black box. Use the pthread primitives the way you are supposed to use them. Then it works. No volatile needed. Just check the POSIX standard.
And SURE the compiler will reload any global variable referenced prior to external calls, but what does that have to do with anything? Do you mean that not needing "volatile" is just a matter of LUCK because phtreads JUST SO HAPPENED to implement pthread_mutex_lock() as a function call?
Maybe that is not a matter of luck? Maybe the implementor was implementing a standard? And he chose this way to implement a (compiler) memory barrier?
Read those two sentences again. They are based directly on the POSIX standard. Is the POSIX standard lying?
Anything CAN be broken, if one tries hard enough.