Re: [PATCH/RFC] mingw: implement PTHREAD_MUTEX_INITIALIZER
- From
Johannes Sixt <j.sixt@viscovery.net>
- Date
- Oct 25, 2011, 15:28 UTC
- 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:
Show 10 quoted lines
> +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