sleep() function gives me problem in a program. I have declared only one mutex, one condition variable and one global variable:
pthread_mutex_t mutex;
pthread_cond_t something1;
int protected = 1;
After initializing them, and creating the 2 threads inside the main with pthread_create
, i write this:
void *Thread(void *arg)
{
while(1){
pthread_mutex_lock(&mutex);
while(protected == 0){
pthread_cond_wait(&something1, &mutex);
}
printf("aaa");
sleep(2);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *Thread2(void *arg){
while(1){
pthread_mutex_lock(&mutex);
while(protected == 1){
pthread_cond_wait(&something1, &mutex);
}
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
This should just print "aaa" in loop forever, and this works if I remove the sleep(2)
.
If I leave it, the program starts, stays alive, but it prints nothing.
Any ideas why this happens?