我有一个监视事物的循环,我使用pthread_cond_timedwait进行阻止。 它将首先每10秒运行一次,但是当它抛出错误22(无效参数)时它会一直运行而不等待。 EINVAL意味着我的abstime错误[http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_timedwait.html][1]。 以下是我的代码
while (!getStopFlag()) {
pthread_mutex_lock(&monitor_lock);
gettimeofday(&now, NULL);
time_t sec = now.tv_sec + updateSec;
long nsec = now.tv_usec * 1000;
sec += nsec / 1000000000L;
nsec = nsec % 1000000000L;
outtime.tv_sec = sec;
outtime.tv_nsec = 0;
ret = pthread_cond_timedwait(&monitor_condition, &monitor_lock, &outtime);
pthread_mutex_unlock(&monitor_lock);
}
不知道为什么最初几次没问题而且之后失败了。 我也试试
clock_gettime(CLOCK_REALTIME, &now);
now.tv_sec += updateSec;
ret = pthread_cond_timedwait(&monitor_condition, &monitor_lock, &now);
和
outtime.tv_sec = time(0) + updateSec;
outtime.tv_nsec = 0;
ret = pthread_cond_timedwait(&monitor_condition, &monitor_lock, &outtime);