日期:2014-05-16 浏览次数:20968 次
#include <stdio.h>
#include <time.h>
#include <pthread.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int main(int argc, char *argv[])
{
struct timespec ts;
ts.tv_sec = 5 + time(NULL);
ts.tv_nsec = 0;
pthread_mutex_lock(&mutex);
system("date '+%H:%M:%S'");
pthread_cond_timedwait(&cond, &mutex, &ts);
system("date '+%H:%M:%S'");
pthread_mutex_unlock(&mutex);
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += 5;
pthread_mutex_lock(&mutex);
system("date '+%H:%M:%S'");
pthread_cond_timedwait(&cond, &mutex, &ts);
system("date '+%H:%M:%S'");
pthread_mutex_unlock(&mutex);
return 0;
}