On Wed, Oct 26, 2016 at 1:26 PM, Jeff King <p...@peff.net> wrote:
> On Wed, Oct 26, 2016 at 10:25:38PM +0200, Johannes Sixt wrote:
>
>> Am 26.10.2016 um 21:51 schrieb Stefan Beller:
>> > it is
>> > very convenient to not have to explicitly initialize mutexes?
>>
>> Not to initialize a mutex is still wrong for pthreads.
>
> I think Stefan was being loose with his wording. There would still be an
> initializer, but it would be a constant (and in the case of pthread
> emulation on Windows, would just be NULL).

Exactly, so we would do

/* as per the man page of pthread_mutexes: */
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;

int somefunction()
{
    pthread_mutex_lock(&mymutex); /* threadsafely initialised on first use */
    ...
    pthread_unlock(&mymutex);
}

and for the Windows compat we'd do


#define PTHREAD_MUTEX_INITIALIZER NULL
#define pthread_mutex_lock emulate_pthread_mutex_lock

int emulate_pthread_mutex_lock(volatile MUTEX_TYPE *mx)
{
    if (*mx == NULL) /* static initializer? */
    { /* this stackoverflow magic to initialize threadsafely if not init'd */}

    EnterCriticalSection(mx) /* as it currently is in compat/win32/pthread.h */
    return 0;
}






>
> -Peff

Reply via email to