gpt4 book ai didi

c++ - 我可以使用 now() 将 wait_until 转换为 wait_for 吗?

转载 作者:搜寻专家 更新时间:2023-10-31 02:12:50 25 4
gpt4 key购买 nike

我正在实现自己的 Future 类,该类建立在第 3 方等待函数 thirdPartyWait(int milliseconds) 之上。我希望我的 Futurestd::future 接口(interface)兼容,这意味着我想同时提供 wait_for wait_until.

wait_for 很简单,因为它直接映射到 thirdPartyWait(我可以在其中处理到毫秒的转换)。但是,wait_until 映射得不是很好。直觉上,我会假设以下应该做我想做的:

template <class Clock, class Duration>
std::future_status wait_until(const std::chrono::time_point<Clock, Duration> &timeout) const
{
return wait_for(timeout - Clock::now());
}

我已经测试了代码并且它有效,但我很清楚一个简单的测试并不能涵盖所有极端情况。我对 stdchrono 部分还不够熟悉,因此我的问题是:

在我的 wait_until 实现中是否存在任何隐藏的陷阱,或者我是否可以期望它按预期工作?

最佳答案

cppreference 对 std::future::wait_until 有这样的说法:

The clock tied to timeout_time is used, which is not required to be a monotonic clock.There are no guarantees regarding the behavior of this function if the clock is adjusted discontinuously, but the existing implementations convert timeout_time from Clock to std::chrono::system_clock and delegate to POSIX pthread_cond_timedwait so that the wait honors ajustments to the system clock, but not to the the user-provided Clock. In any case, the function also may wait for longer than until after timeout_time has been reached due to scheduling or resource contention delays.

从中我推断当前的实现与您所做的完全一样。

http://en.cppreference.com/w/cpp/thread/future/wait_until

关于c++ - 我可以使用 now() 将 wait_until 转换为 wait_for 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42225152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com