Quote:
Originally Posted by
Corona688
Try 'volatile int lock'. If you're going to modify a variable out from under something else in mysterious ways like threading or memory poking, you have to tell the compiler so. Otherwise it feels free to optimize it away on the assumption that it won't change until it changes it itself.
You should really be using mutexes, though.
Yes, you should use mutex. volatile alone (along with atomic operation) is not enough to guarantee correctness in multi-threaded program. The magic word here is: memory visibility. Interested readers might want to read:
Mutex and Memory Visibility
Cheers,
Loïc.
---------- Post updated at 10:15 AM ---------- Previous update was at 10:07 AM ----------
Quote:
Originally Posted by
mrhosseini
thanks,
yes, that was just an example that i used to test the threads behavior.
so the mutexes and any other data that i want to be shared must be defined as volatile, am i right?
No, absolutely not. As long as you use mutex correctly, there is no need to declare the variables (or the mutexes) volatile.
On a conforming POSIX platform, the compiler shall "know" how to deal with that kind of issue as long as you follow the memory visibility rules given in XBD 4.11 of
IEEE Std 1003.1-2008.
Cheers,
Loïc.