Note: This is a beta release of Red Hat Bugzilla 5.0. The data contained within is a snapshot of the live data so any changes you make will not be reflected in the production Bugzilla. Also email is disabled so feel free to test any aspect of the site that you want. File any problems you find or give feedback here.
Bug 3862 - timeout of pthread_cond_timedwait corrupts condition variable
Summary: timeout of pthread_cond_timedwait corrupts condition variable
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: glibc
Version: 6.0
Hardware: i386
OS: Linux
Target Milestone: ---
Assignee: Cristian Gafton
QA Contact:
Depends On:
TreeView+ depends on / blocked
Reported: 1999-07-01 21:00 UTC by ham
Modified: 2008-05-01 15:37 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Last Closed: 2000-01-05 01:40:38 UTC

Attachments (Terms of Use)

Description ham 1999-07-01 21:00:03 UTC
If a thread blocks on pthread_cond_timedwait, and other
threads block on pthread_cond_wait and the first thread
times out, a pthread_cond_signal will fail to wake any

Here is a piece of sample code:


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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

char buf[512];

int timeout = 1;

void *transient_worker (void *arg)
   struct timespec tspec;


   while (1) {
      tspec.tv_sec = time(NULL) + timeout;
      tspec.tv_nsec = 0;

      if (pthread_cond_timedwait(&cond, &mutex, &tspec) ==

      printf("transient worker: %s\n", buf);


void *worker (void *arg)

   while (1) {
      pthread_cond_wait(&cond, &mutex);

      printf("worker: %s\n", buf);

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

   if (argc > 1)
      timeout = atoi(argv[1]);

   pthread_create(&tid, NULL, transient_worker, NULL);
   pthread_create(&tid, NULL, worker, NULL);

   while (1) {

      scanf("%s", buf);




Ignore the synchronization of "buf" - it's not really an
issue for this example.

If you compile this code and run it with no arguments and
then type a string, nothing is printed out because the
transient worker has exited. However, if you run it with an
argument of say, 20, and then type in a string it then works
fine. Then if you wait 20 seconds before typing in anything
else, the transient thread will die and then it will work
correctly with the one remaining thread. If you type
something in before the 20 seconds is up, then the transient
thread becomes the first thread waiting on the condition
variable and when it dies the program fails...

Comment 1 ham 1999-07-06 15:36:59 UTC
I tested the program on an older Red Hat distribution and it worked

The libraries that it failed for are:

It worked with the following libraries:

Comment 2 Cristian Gafton 2000-01-05 01:40:59 UTC
The program seems to work correctly for me with glibc 2.1.3 - which will be
available on this week's rawhide.

Note You need to log in before you can comment on or make changes to this bug.