在线文档教程
C++
线程支持 | Thread support

std::future::wait_until

STD::未来:等待[医]直到

template< class Clock, class Duration > std::future_status wait_until( const std::chrono::time_point& timeout_time ) const;(since C++11)

wait_until等待结果可用。它阻塞直到指定。timeout_time已达到或结果可用,以第一位为准。返回值指示为什么wait_until回来了。

如果valid()== false在调用此函数之前。

参数

timeout_time-maximum time point to block until

返回值

ConstantExplanation
future_status::deferredThe function to calculate the result has not been started yet
future_status::readyThe result is ready
future_status::timeoutThe timeout has expired

例外

任何由时钟、时间引发的异常[医]在执行%28时钟、时间点和标准库提供的持续时间期间,不要抛出%29。

注记

鼓励实现在下列情况下检测情况:valid == false在呼叫前抛出一个future_error错误条件为future_errc::no_state...

钟系在timeout_time使用,这不需要是一个单调的时钟。如果时钟不连续地调整,则不能保证此函数的行为,但是现有的实现转换。timeout_timeClockstd::chrono::system_clock并委派给POSIX螺纹[医]康德[医]等待时间这样,等待就会尊重系统时钟,而不会尊重用户提供的时钟。Clock在任何情况下,该功能也可能等待更长的时间。timeout_time由于调度或资源争用延迟已到达。

二次

#include <iostream> #include <future> #include <thread> #include <chrono> int main() { std::chrono::system_clock::time_point two_seconds_passed = std::chrono::system_clock::now() + std::chrono::seconds(2 // Make a future that that takes 1 second to completed 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 that takes 5 seconds to completed 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!

二次

另见

waitwaits for the result to become available (public member function)
wait_forwaits for the result, returns if it is not available for the specified timeout duration (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/线程/前途/等待[医]直到