This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

loop in pthread_cond_broadcast


Below is a sample program that illustrates a loop in pthread_cond_broadcast()
when called the second time.  CPU utilization goes to 100% and the function
never returns.

Thanks, Greg

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

void *baby();

pthread_cond_t  cond;
pthread_mutex_t lock;
pthread_attr_t  attr;
pthread_t       tid;

int main ()
{
int i;

   pthread_cond_init(&cond, NULL);
   pthread_attr_init(&attr);
   pthread_mutex_init(&lock, NULL);

   pthread_mutex_lock(&lock);
   pthread_create(&tid, &attr, baby, NULL);

   for (i=0; i<10; i++)
   {  sleep(1);
      printf("before cond wait\n");
      pthread_cond_wait(&cond, &lock);
      printf("after  cond wait\n");
   }
   pthread_mutex_unlock(&lock);
}

void *baby()
{
   while (1)
   {  sleep(2);
      pthread_mutex_lock(&lock);
      printf("before cond broadcast\n");
      pthread_cond_broadcast(&cond);
      printf("after  cond broadcast\n");
      pthread_mutex_unlock(&lock);
   }
}

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

gsmith@GREG ~
$ ./ptbarf
before cond wait
before cond broadcast
after  cond broadcast
after  cond wait
before cond wait
before cond broadcast

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]