delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2001/06/14/15:32:52

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT sources DOT redhat DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sources DOT redhat DOT com>
List-Help: <mailto:cygwin-help AT sources DOT redhat DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT sources DOT redhat DOT com
Delivered-To: mailing list cygwin AT sources DOT redhat DOT com
Message-ID: <3B290FE5.22678B61@trex.rtpnc.epa.gov>
Date: Thu, 14 Jun 2001 15:26:29 -0400
From: Greg Smith <rys AT epaibm DOT rtpnc DOT epa DOT gov>
X-Mailer: Mozilla 4.74 [en] (WinNT; U)
X-Accept-Language: en
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: hang in pthread_cond_signal

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 AT 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 AT GREG ~
$ gcc -o ptbarf ptbarf.c

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

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

GSmith AT 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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019