|
From: David F. <fa...@kd...> - 2013-05-16 18:29:38
|
On Tuesday 14 May 2013 20:18:44 Phil Longstaff wrote: > int* my_ptr = new int; > *my_ptr = 10; > pthread_mutex_lock(&lock); > shared_ptr = my_ptr; > pthread_mutex_unlock(&lock); > > Thread 2: > pthread_mutex_lock(&lock); > int* my_ptr = shared_ptr; > pthread_mutex_unlock(&lock); > ... = *my_ptr; You're reading a region of memory outside mutex protection, and that region of memory was written to, outside mutex protection. That's the basic definition of a data race. Getting the address of that region of memory within the mutex doesn't change that. You see it as non-racy because "how could *my_ptr ever be something else than 10" ... but if you think about a multi-processor system, the write of the value 10 might not get propagated to the cache of the other processor where the read happens, since the system had no reason to perform that synchronisation. -- David Faure, fa...@kd..., http://www.davidfaure.fr Working on KDE, in particular KDE Frameworks 5 |