hang in pthread_cond_signal

Greg Smith rys@epaibm.rtpnc.epa.gov
Thu Jun 14 12:26:00 GMT 2001


I am using the cygwin-src snapshot from June 10.

Seems pthread_cond_signal can hang while another thread
is waiting on the condition AND a pthread_cond_signal
has been previously issued when no one was waiting on the
condition.  Below is a testcase that illustrates the
problem:

Thanks,

Greg


GSmith@GREG ~
$ cat ptbarf.c
#include <stdio.h>
#include <errno.h>
#include <pthread.h>

pthread_cond_t cond;
pthread_mutex_t lock;

void *t1();

int main()
{ pthread_t tid;
  void *ret;

    if (pthread_cond_init(&cond, NULL)) {
        printf("init failed for cond: %s\n", errno);
        return;
    }

    if (pthread_mutex_init(&lock, NULL)) {
        printf("init failed for lock: %s\n", errno);
        return;
    }

    if (pthread_mutex_lock(&lock)) {
        printf("main lock failed: %s\n", errno);
        return;
    }
    printf("main has lock\n");

#ifdef DEBUG
//  issue spurious cond_signal to cause the problem
    if (pthread_cond_signal(&cond)) {
        printf("main signal failed: %s\n", errno);
        return;
    }
#endif

    if (pthread_create(&tid, NULL, t1, NULL)) {
        printf("create for t1 failed: %s\n", errno);
        return;
    }

    printf("main waiting\n");
    if (pthread_cond_wait(&cond, &lock)) {
        printf("main wait failed: %s\n", errno);
        return;
    }

    if (pthread_mutex_unlock(&lock)) {
        printf("main unlock failed: %s\n", errno);
        return;
    }
    printf("main released lock\n");

    if (pthread_join(tid, &ret)) {
        printf("join for t1 failed: %s\n", errno);
        return;
    }
    printf ("main terminating\n");
}

void *t1()
{
    sleep(5);

    printf("t1 getting lock\n");
    if (pthread_mutex_lock(&lock)) {
        printf("t1 lock failed: %s\n", errno);
        return;
    }

    printf("t1 signalling cond\n");
    if (pthread_cond_signal(&cond)) {
        printf("t1 signal failed: %s\n", errno);
        return;
    }

    printf("t1 releasing lock\n");
    if (pthread_mutex_unlock(&lock)) {
        printf("t1 unlock failed: %s\n", errno);
        return;
    }
    printf ("t1 terminating\n");
}

GSmith@GREG ~
$ gcc -o ptbarf ptbarf.c

GSmith@GREG ~
$ ./ptbarf
main has lock
main waiting
t1 getting lock
t1 signalling cond
t1 releasing lock
main released lock
t1 terminating
main terminating

GSmith@GREG ~
$ gcc -DDEBUG -o ptbarf ptbarf.c

GSmith@GREG ~
$ ./ptbarf
main has lock
main waiting
t1 getting lock
t1 signalling cond

--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list