Subj : Re: posix and lock-free algorithms To : comp.programming.threads From : peter.koch.larsen Date : Mon Aug 01 2005 06:01 am David Schwartz skrev: > "John Doug Reynolds" wrote in message > news:1122829815.237744.74450@o13g2000cwo.googlegroups.com... > > > I get the idea that the issue is that Posix does not provide explicit > > memory barriers. If so, then there must be some reason why this > > construct does not do the trick: > > > > void memory_barrier() { mutex m; m.lock(); m.unlock(); } > > Mutexes only protect memory access that occur while the mutex is held. > In principle, that could be optimized by a sufficiently smart implementation > to nothing. Hi David Could you elaborate on this? Do you mean to imply that objects that might be in two states, where one state assumes that no locking is required and the other one does are completely broken? That scenario is quite common I believe, e.g. having the object be in the non-locking phase during construction. object* o = create(...); // no mutex! o->some_extra_initialisation(); // no mutex! make_available_to_others(o); // e.g. insertion into some shared queue If some other thread grabs o and uses it - could it be that it will not see the "correct" image of o? /Peter .