pthread_cond_timedwait()不等于1秒钟? (Linux)的

时间:2014-05-29 09:28:54

标签: linux pthreads wait milliseconds

我写了很好的能力来收听UDP消息,每当有新消息到达时,这些消息都会被添加到FIFO中,并且会向监听器发出信号。

如果没有其他任何操作,则侦听器会等待消息。但是,在某些情况下,它知道它应该在很短的时间内醒来。所以我编写了使用pthread_cond_timedwait()的代码,并在那里把时间在我当前的测试中大约1.5秒。

等待1秒钟,然后等待功能不再阻止。这是否意味着当前的实现不支持亚秒(毫秒/微秒等待)?

我的输出有一点点。我从1417毫秒开始,似乎在第一次尝试时等待1001毫秒。然后在每次下一次尝试时为0或1毫秒,完全不阻塞。

image transform ending with [1416272]
wait for 1417 till 1401354361 now = 1401354360
image transform ending with [415259]
wait for 416 till 1401354361 now = 1401354361
image transform ending with [414759]
wait for 415 till 1401354361 now = 1401354361
image transform ending with [414196]
wait for 415 till 1401354361 now = 1401354361
image transform ending with [413646]
wait for 414 till 1401354361 now = 1401354361
image transform ending with [413013]
wait for 414 till 1401354361 now = 1401354361
image transform ending with [412385]
wait for 413 till 1401354361 now = 1401354361
image transform ending with [411801]
wait for 412 till 1401354361 now = 1401354361
image transform ending with [411237]
wait for 412 till 1401354361 now = 1401354361
image transform ending with [410690]
wait for 411 till 1401354361 now = 1401354361
image transform ending with [410204]
wait for 411 till 1401354361 now = 1401354361
image transform ending with [409728]
wait for 410 till 1401354361 now = 1401354361
image transform ending with [409150]
wait for 410 till 1401354361 now = 1401354361
image transform ending with [408566]
wait for 409 till 1401354361 now = 1401354361
image transform ending with [408004]
...snip...
wait for 3 till 1401354361 now = 1401354361
image transform ending with [2188]
wait for 3 till 1401354361 now = 1401354361
image transform ending with [1628]
wait for 2 till 1401354361 now = 1401354361
image transform ending with [1077]
wait for 2 till 1401354361 now = 1401354361
image transform ending with [221]
wait for 1 till 1401354361 now = 1401354361

等待功能:

    void wait(int msecs)
    {
        if(msecs == -1)
        {
            pthread_cond_wait(&f_condition, &f_mutex.f_mutex);
        }
        else if(msecs > 0)
        {
            struct timeval tod;
            gettimeofday(&tod, nullptr);
            struct timespec ts;
            ts.tv_sec = tod.tv_sec + msecs / 1000;
            ts.tv_nsec = tod.tv_usec * 1000 + msecs % 1000;
            ts.tv_sec += ts.tv_nsec / 1000000000L;
            ts.tv_nsec = ts.tv_nsec % 1000000000L;

std::cerr << "wait for " << msecs << " till " << ts.tv_sec << " now = " << time(NULL) << "\n";

            pthread_cond_timedwait(&f_condition, &f_mutex.f_mutex, &ts);
        }
    }

1 个答案:

答案 0 :(得分:0)

  

我写了很好的能力来收听UDP消息,每当有新消息到达时,这些消息都会被添加到FIFO中,并且会向监听器发出信号。

好吧,套接字缓冲区已经是FIFO了。因此,您从FIFO获取UDP数据报并再次将其放入FIFO中。是吗?

  

如果没有其他任何操作,则侦听器会等待消息。但是,在某些情况下,它知道它应该在很短的时间内醒来。所以我编写了使用pthread_cond_timedwait()的代码,并在那里把时间在我当前的测试中大约1.5秒。

为什么不使用select/epoll或类似libevent的内容,1.5秒超时?

  

等待1秒钟,然后等待功能不再阻止。这是否意味着当前的实现不支持亚秒(毫秒/微秒等待)?

您的输出难以阅读,无助于确定发生了什么。您需要打印您想要等待多长时间,发生了什么(超时或接收到信号),以及在超时的情况下,它实际等待了多长时间。