bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: The semantics of pthread_cond_wait


From: Richard Braun
Subject: Re: The semantics of pthread_cond_wait
Date: Thu, 16 Aug 2012 12:26:55 +0200
User-agent: Mutt/1.5.20 (2009-06-14)

On Thu, Aug 16, 2012 at 11:48:35AM +0200, Richard Braun wrote:
> On Wed, Aug 15, 2012 at 05:47:47PM -0700, Thomas DiModica wrote:
> > My understanding is that pthread_cond_wait is a cancellation point.
> > It achieves this by entering asynchronous cancellation mode before blocking.
> > 
> > I don't see, however, any code that checks for a pending cancellation when
> > we enter the function. As far as I can tell, the implementation is that
> > pthread_cond_wait is a cancellation point if and only if the thread is
> > canceled while it is blocked.
> 
> It seems so, yes.

This tiny test shows the bug :

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int predicate;
int cancelled;

static void *
run(void *arg)
{
    printf("spin until cancellation is sent\n");

    while (!cancelled)
        sched_yield();

    printf("waiting for predicate\n");

    pthread_mutex_lock(&mutex);

    while (!predicate)
        pthread_cond_wait(&cond, &mutex);

    pthread_mutex_unlock(&mutex);

    printf("exiting thread\n");
    return NULL;
}

int
main(int argc, char *argv[])
{
    pthread_t thread;

    pthread_create(&thread, NULL, run, NULL);
    printf("sending cancellation\n");
    pthread_cancel(thread);
    cancelled = 1;

    printf("joining thread\n");
    pthread_join(thread, NULL);
    return EXIT_SUCCESS;
}

-- 
Richard Braun



reply via email to

[Prev in Thread] Current Thread [Next in Thread]