Consider a simple (global in my case) variable:
int i;
Somewhere this variable is accessed
pthread_mutex_lock(i_mutex);
if(i == other value) {
do_something();
}
pthread_mutex_unlock(i_mutex);
Another thread updates i while it holds i_mutex . Could the compiler cache the value of i so
I don’t get the recent value ? Must i be volatile ?
pthread locks implement memory barriers that will ensure that cache effects are made visible to other threads. You don’t need volatile to properly deal with the shared variable
iif the accesses to the shared variable are protected by pthread mutexes.from http://www.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_11: