On 10/28/07, skaller <[EMAIL PROTECTED]> wrote: > > On Sun, 2007-10-28 at 18:29 +0100, Richard Guenther wrote: > > On 10/28/07, Erik Trulsson <[EMAIL PROTECTED]> wrote: > > > > Unfortunately it seems that the POSIX standard for threads say that as > > > long > > > as access to a shared variable is protected by a mutex there is no need to > > > use 'volatile'. > > > > Which is a very unpracticable say, as it essentially would force the > > compiler > > to assume every variable is protected by a mutex (how should it prove > > otherwise?) > > So the proof is easy: mutex ops are function calls, > assume all function calls lock or unlock. > > Thus: store registers aliasing sharable variables into > those variables on every function call. > > int x = 1; > x = x + 1; // r0 <- x; r0++ > x = x + 1; // r0++; > f(); // x <- r0; f(); > > Note: this is not well stated. There is no explicit coupling > between a given variable and a mutex. > > If thread A locks Mutex MA, and B locks MB, there is no synchronisation > between these threads and sharing can fail: it has to be the same > mutex (to effect 'mutual exclusion'). > > When two threads are exclusive, it is safe to keep variables > in registers again (because the other thread is locked up). > > OK .. hmm .. well this is the idea, but a more formal proof > would be cool.
Doesn't work: int a; void foo(bool locked) { if (locked) a++; } void bar(void) { pthread_mutex_lock (&mx); foo(true); pthread_mutex_unlock(&mx); } you cannot do such analysis without seeing the whole program. Richard.