正确实施condition_variable timed_wait

Implementing condition_variable timed_wait correctly

本文关键字:variable wait timed condition      更新时间:2023-10-16

我正在阅读我的 STL 实现(标准问题 g++ 4.6.2 ),并在 condition_variable 中遇到了以下竞争条件:

template<typename _Rep, typename _Period>
cv_status
wait_for(unique_lock<mutex>& __lock,
         const chrono::duration<_Rep, _Period>& __rtime)
{
    return wait_until(__lock, __clock_t::now() + __rtime);
}

因为__clock_t是一个std::chrono::system_clock,我们被NTP之类的东西所束缚(如果时钟在__clock_t::now() + __rtime后向后移动一天,那么我们将等待一天)。

C++标准(30.5.1)似乎做对了:

26

效果:仿佛

return wait_until(lock, chrono::steady_clock::now() + rel_time);

Boost的condition_variable实现也有同样的问题:

template<typename duration_type>
bool timed_wait(unique_lock<mutex>& m,duration_type const& wait_duration)
{
    return timed_wait(m,get_system_time()+wait_duration);
}

事实上,底层的 pthreads 实现似乎是问题所在:

int pthread_cond_timedwait(pthread_cond_t *restrict cond,
   pthread_mutex_t *restrict mutex,
   const struct timespec *restrict abstime);

因为abstime被指定为"系统时间",而不是单调时钟。

所以我的问题是:如何正确实现像std::condition_variable::wait_for这样的东西?是否有现有的实现可以做到这一点?还是我错过了什么?

诀窍

是使用pthread_condattr_setclock告诉pthread_condattr_t使用CLOCK_MONOTONIC。执行此操作的 C 代码非常简单:

#include <time.h>
#include <pthread.h>
#include <errno.h>
#include <stdio.h>
int main()
{
    // Set the clock to be CLOCK_MONOTONIC
    pthread_condattr_t attr;
    pthread_condattr_init(&attr);
    if (int err = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC))
    {
        printf("Error setting clock: %dn", err);
    }
    // Now we can initialize the pthreads objects with that condattr
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t  cond;
    pthread_cond_init(&cond, &attr);
    // when getting the time, we must poll from CLOCK_MONOTONIC
    struct timespec timeout;
    struct timespec now;
    clock_gettime(CLOCK_MONOTONIC, &now);
    timeout.tv_sec = now.tv_sec + 5;
    timeout.tv_nsec = now.tv_nsec;
    // business as usual...
    pthread_mutex_lock(&mutex);
    int rc = pthread_cond_timedwait(&cond, &mutex, &timeout);
    if (rc == ETIMEDOUT)
        printf("Success!n");
    else
        printf("Got return that wasn't timeout: %dn", rc);
    pthread_mutex_unlock(&mutex);
    return 0;
}

我将暂时保持开放状态,因为有人可能会有更简单的答案。我在这里不满意的是,这意味着wait_until很难用实时时钟实现(我最好的解决方案是将time_point中提供的Clock转换为steady_clock的时间,然后从那里开始......它仍然受时间变化竞争条件的影响,但如果你实时指定超时,你已经犯了一个可怕的错误)。

相关文章: