From: Johannes Sixt Date: Tue, 25 Oct 2011 15:28:20 GMT Subject: Re: [PATCH/RFC] mingw: implement PTHREAD_MUTEX_INITIALIZER Message-ID: <4EA6D594.90402@viscovery.net> In-Reply-To: <1319554509-6532-1-git-send-email-kusmabite@gmail.com> Am 10/25/2011 16:55, schrieb Erik Faye-Lund: > +int pthread_mutex_lock(pthread_mutex_t *mutex) > +{ > + if (mutex->autoinit) { > + if (InterlockedCompareExchange(&mutex->autoinit, -1, 1) != -1) { > + pthread_mutex_init(mutex, NULL); > + mutex->autoinit = 0; > + } else > + while (mutex->autoinit != 0) > + ; /* wait for other thread */ > + } The double-checked locking idiom. Very suspicious. Can you explain why it works in this case? Why are no Interlocked functions needed for the other accesses of autoinit? ("It is volatile" is the wrong answer to this last question, BTW.) -- Hannes