Thread (57 messages) 57 messages, 7 authors, 2016-10-27

Re: [PATCH 27/36] attr: convert to new threadsafe API

From: Stefan Beller <hidden>
Date: 2016-10-26 20:43:31

On Wed, Oct 26, 2016 at 1:26 PM, Jeff King [off-list ref] wrote:
On Wed, Oct 26, 2016 at 10:25:38PM +0200, Johannes Sixt wrote:
quoted
Am 26.10.2016 um 21:51 schrieb Stefan Beller:
quoted
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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help