how can use the function pthread_cond_wait to synchronize thread?
I write one example as below:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sched.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <unistd.h>
pthread_t tid1,tid2;
pthread_mutex_t mutex;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *thread1()
{
while (1)
{
pthread_cond_wait( &cond,& mutex );
printf("\nthread1 get lock!\n");
pthread_mutex_unlock( &mutex );
sleep (5);
}
pthread_exit(NULL);
}
int main()
{
pthread_mutex_init(&mutex,NULL );
pthread_cond_init( &cond,NULL);
pthread_create(&tid1,NULL,thread1,NULL);
while(1)
{
pthread_cond_signal(&cond);
sleep (60);
}
return 0;
}
I want to do sleep 60s and then let thread1 runs one time,but the result is the thread runs every 5s,the condi is useless,why?