pthread_cond_timedwait()

前端 未结 7 1571
有刺的猬
有刺的猬 2020-12-16 05:22
void wait(int timeInMs)
{
    struct timespec timeToWait;
    timeToWait.tv_sec = 5;
    timeToWait.tv_nsec = timeInMs*1000;

    int rt;

    pthread_mutex_lock(&am         


        
7条回答
  •  余生分开走
    2020-12-16 05:58

    That code doesn't sleep, it checks a condition for a while. As you are not probably setting cond ok it just returns immediately.

    If you are not willing to synchronize threads around a signal then pthread_cond _wait is not what you need. Check here how condition variables work.

    if you want to sleep with seconds precision use sleep

    If you want to sleep with microseconds precision use select with timevals.

提交回复
热议问题