pthread_cond_timedwait在Android中实际超时之前返回ETIMEDOUT

时间:2017-09-11 04:24:33

标签: android c++ pthreads

我使用pthread_cond_timewait来阻塞线程给定时间或让它在Andorid本机C ++代码中等待信号,下面是我的等待函数。但是在Android O中,等待函数可能会在超时之前过早地返回ETIMEDOUT。我不明白为什么会这样。

int Wait(MutexLock& mutex, int64_t misc)
{
    int iret=-1; 
    if(misc<=0)
    {
        iret = pthread_cond_wait(&cond_, &mutex.get_pthread_mutex());
        return iret;
    }
    struct timespec ts;
    timeval now; 
    gettimeofday(&now,NULL);
    ts.tv_sec = now.tv_sec;
    ts.tv_nsec = now.tv_usec * 1000;
    ts.tv_sec +=misc/1000;
    uint64_t cur_misc=ts.tv_nsec/1000000;
    uint64_t tmp_misc=cur_misc+misc%1000;
    ts.tv_sec +=tmp_misc/1000;
    ts.tv_nsec =(tmp_misc%1000)*1000000;   
    iret=pthread_cond_timedwait(&cond_, &mutex.get_pthread_mutex(), &ts);
    return iret;
}

0 个答案:

没有答案