pthread_cond_timedwait 用于在同时等待条件变量时提供超时功能,不过该函数的超时时间是一个绝对时间。默认使用系统时间,这意味这,若修改系统时间,那么超时就不准确,有可能提前返回,也可能要几年才返回。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
int main()
{
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
pthread_condattr_t attr;
pthread_cond_t cond;
pthread_condattr_init(&attr);
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
pthread_cond_init(&cond, &attr);
struct timespec tv;
pthread_mutex_lock(&m);
do{
clock_gettime(CLOCK_MONOTONIC, &tv);
tv.tv_sec += 1;
pthread_cond_timedwait(&cond,&m,&tv);
printf("heart beat\n");
}while(1);
pthread_mutex_unlock(&m);
return 0;
}