Subject: Re: Two questions
To: None <>
From: Mihai CHELARU <>
List: current-users
Date: 04/28/2003 13:32:19
On Monday 28 April 2003 10:34, Mihai CHELARU wrote:
> 2) Anyone is experiencing problems using threads and time wait
> functions like nanosleep (or usleep) and pthread_cond_timedwait ?
> Using those in a code results in infinite (sa)wait. Could be my poor
> code or something else, just want to check.

Here is a smaller code that is reproducing the situation. The code hangs 
at first usleep (100000) in th2() so you'll not be able to see any 
THREAD: Leaving....

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>

pthread_mutex_t m;
pthread_cond_t c;

void *th2 (void *x)
int i=(*(int*)x);
pthread_cond_signal (&c);
while (i>0) {
        printf ("THREAD: Ok. pause for %d\n",i);
        usleep (100000);
        printf ("THREAD: Leaving %d\n",i);
        usleep (100000);

main ()
pthread_t t;
int i;
pid_t p;

for (;;) {
        p=fork ();
        if (p<=0) break;
        waitpid (p,NULL,0);
        return 0;
pthread_mutex_init (&m, NULL);
pthread_cond_init (&c, NULL);
for (i=0;i<255;i++) {
        pthread_create (&t, NULL, th2, (void*)(&i));
        pthread_detach (t);
        pthread_mutex_lock (&m);
        pthread_cond_wait (&c, &m);
        pthread_mutex_unlock (&m);
sleep (2000);
return 0;