Bug 3862 - timeout of pthread_cond_timedwait corrupts condition variable
timeout of pthread_cond_timedwait corrupts condition variable
Status: CLOSED RAWHIDE
Product: Red Hat Linux
Classification: Retired
Component: glibc (Show other bugs)
6.0
i386 Linux
high Severity high
: ---
: ---
Assigned To: Cristian Gafton
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 1999-07-01 17:00 EDT by ham
Modified: 2008-05-01 11:37 EDT (History)
1 user (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2000-01-04 20:40:38 EST
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description ham 1999-07-01 17:00:03 EDT
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
thread.

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;

   pthread_mutex_lock(&mutex);

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

      if (pthread_cond_timedwait(&cond, &mutex, &tspec) ==
ETIMEDOUT)
         break;

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

   pthread_mutex_unlock(&mutex);
}

void *worker (void *arg)
{
   pthread_mutex_lock(&mutex);

   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);

      pthread_cond_signal(&cond);

   }
}

/**********************************************************/

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 11:36:59 EDT
I tested the program on an older Red Hat distribution and it worked
correctly.

The libraries that it failed for are:
        libc-2.1.1.so
        libpthread-0.8.so

It worked with the following libraries:
        libc-2.0.7.so
        libpthread-0.7.so
Comment 2 Cristian Gafton 2000-01-04 20:40:59 EST
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.