From: Johannes Sixt Date: Tue, 25 Oct 2011 21:13:34 GMT Subject: Re: [msysGit] Re: [PATCH/RFC] mingw: implement PTHREAD_MUTEX_INITIALIZER Message-ID: <4EA7267E.1080103@kdbg.org> In-Reply-To: Am 25.10.2011 22:51, schrieb Erik Faye-Lund: > On Tue, Oct 25, 2011 at 10:07 PM, Johannes Sixt wrote: >> HOWEVER, when it continues, there is NO [*] guarantee that it will also >> see the values that InitializeCriticalSection() has written, because >> there were no memory barriers involved. When it continues, there is a >> chance that it calls EnterCriticalSection() with uninitialized values! >> > > Thanks for pointing this out, I completely forgot about write re-ordering. > > This is indeed a problem. So, shouldn't replacing "mutex->autoinit = > 0;" with "InterlockedExchange(&mutex->autoinit, 0)" solve the problem? > InterlockedExchange generates a full memory barrier: > http://msdn.microsoft.com/en-us/library/windows/desktop/ms683590(v=vs.85).aspx That should do it. >> [*] If you compile this code with MSVC >= 2005, "No guarantee" is not >> true, it's exactly the opposite because Microsoft extended the meaning >> of 'volatile' to imply a memory barriere. > > Do you have a source for this? I'm not saying it isn't true, I just > never heard of this, and would like to read up on it :) http://msdn.microsoft.com/en-us/library/ms686355%28VS.85%29.aspx -- Hannes