Vin*_*ent 11 c c++ linux synchronization pthreads
pthread_cond_timedwait函数需要timepec结构中的绝对时间.
我想用什么时间来获得绝对时间.我在网上看到了很多例子,我发现几乎所有时间都使用了功能.(ftime,clock,gettimeofday,clock_gettime(所有可能的CLOCK _...).
pthread_cond_timedwait使用绝对时间.这个等待时间是否会因改变机器的时间而受到影响?此外,如果我使用其中一个时间函数获得绝对时间,如果机器的时间在获取和添加增量时间之间发生变化,这将影响等待时间?是否有可能等待相对时间的事件?
jil*_*les 15
要使用的函数是clock_gettime()条件变量的时钟id.这个时钟id为CLOCK_REALTIME默认,但可以改变(如CLOCK_MONOTONIC通过用初始化条件变量)pthread_condattr_t在其上pthread_condattr_setclock()被调用.
使用time()不是一个好主意,因为这不能保证等于tv_seca clock_gettime(CLOCK_REALTIME).在某些情况下,这可能会导致您的程序忙碌 - 等待time()到达clock_gettime(CLOCK_REALTIME)不久前已达到的秒.