命名空间
变体
操作

std::future<T>::wait_until

来自 cppreference.cn
< cpp‎ | thread‎ | future
 
 
并发支持库
线程
(C++11)
(C++20)
this_thread 命名空间
(C++11)
(C++11)
(C++11)
协作取消
互斥
(C++11)
通用锁管理
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
条件变量
(C++11)
信号量
闭锁和屏障
(C++20)
(C++20)
期值
(C++11)
(C++11)
(C++11)
(C++11)
安全回收
(C++26)
Hazard 指针
原子类型
(C++11)
(C++20)
原子类型的初始化
(C++11)(在 C++20 中已弃用)
(C++11)(在 C++20 中已弃用)
内存顺序
(C++11)(在 C++26 中已弃用)
原子操作的自由函数
原子标志的自由函数
 
 
template< class Clock, class Duration >
std::future_status wait_until( const std::chrono::time_point<Clock,Duration>& timeout_time ) const;
(自 C++11 起)

wait_until 等待结果变为可用。它会阻塞,直到指定的 timeout_time 到达或结果变为可用,以较早发生者为准。返回值指示 wait_until 返回的原因。

如果 future 是调用 async 的结果,且该调用使用了延迟求值,则此函数会立即返回,而无需等待。

如果调用此函数之前 valid()false,或者 Clock 不满足 Clock 的要求,则行为未定义。如果 std::chrono::is_clock_v<Clock>false,则程序是非良构的。(自 C++20 起)

目录

[编辑] 参数

timeout_time - 阻塞至此时间点的最大时间

[编辑] 返回值

常量 解释
future_status::deferred 共享状态包含使用延迟求值的延迟函数,因此结果仅在显式请求时计算
future_status::ready 结果已就绪
future_status::timeout 超时已过期

[编辑] 异常

时钟、时间点或持续时间在执行期间抛出的任何异常(标准库提供的时钟、时间点和持续时间永远不会抛出异常)。

[编辑] 注意

鼓励实现检测到在调用之前 valid() == false 的情况,并抛出带有错误条件 std::future_error,错误条件为 future_errc::no_state

标准建议,与 timeout_time 关联的时钟应用于测量时间;该时钟不需要是单调时钟。如果时钟发生不连续调整,则此函数的行为没有保证,但现有的实现将 timeout_timeClock 转换为 std::chrono::system_clock 并委托给 POSIX pthread_cond_timedwait,以便等待尊重对系统时钟的调整,但不尊重用户提供的 Clock。在任何情况下,由于调度或资源争用延迟,该函数也可能等待超过 timeout_time 到达之后的时间。


[编辑] 示例

#include <chrono>
#include <future>
#include <iostream>
#include <thread>
 
int main()
{
    std::chrono::system_clock::time_point two_seconds_passed
        = std::chrono::system_clock::now() + std::chrono::seconds(2);
 
    // Make a future that takes 1 second to complete
    std::promise<int> p1;
    std::future<int> f_completes = p1.get_future();
    std::thread([](std::promise<int> p1)
                { 
                    std::this_thread::sleep_for(std::chrono::seconds(1)); 
                    p1.set_value_at_thread_exit(9); 
                }, 
                std::move(p1)
    ).detach();
 
    // Make a future that takes 5 seconds to complete
    std::promise<int> p2;
    std::future<int> f_times_out = p2.get_future();
    std::thread([](std::promise<int> p2)
                { 
                    std::this_thread::sleep_for(std::chrono::seconds(5)); 
                    p2.set_value_at_thread_exit(8); 
                }, 
                std::move(p2)
    ).detach();
 
    std::cout << "Waiting for 2 seconds..." << std::endl;
 
    if (std::future_status::ready == f_completes.wait_until(two_seconds_passed))
        std::cout << "f_completes: " << f_completes.get() << "\n";
    else
        std::cout << "f_completes did not complete!\n";
 
    if (std::future_status::ready == f_times_out.wait_until(two_seconds_passed))
        std::cout << "f_times_out: " << f_times_out.get() << "\n";
    else
        std::cout << "f_times_out did not complete!\n";
 
    std::cout << "Done!\n";
}

可能的输出

Waiting for 2 seconds...
f_completes: 9
f_times_out did not complete!
Done!

[编辑] 参见

等待结果变为可用
(公共成员函数) [编辑]
等待结果,如果在指定的超时持续时间内不可用则返回
(公共成员函数) [编辑]