std::future::wait_until
STD::未来:等待[医]直到
template< class Clock, class Duration > std::future_status wait_until( const std::chrono::time_point | | (since C++11) |
---|
wait_until
等待结果可用。它阻塞直到指定。timeout_time
已达到或结果可用,以第一位为准。返回值指示为什么wait_until
回来了。
如果valid()
== false
在调用此函数之前。
参数
timeout_time | - | maximum time point to block until |
---|
返回值
Constant | Explanation |
---|---|
future_status::deferred | The function to calculate the result has not been started yet |
future_status::ready | The result is ready |
future_status::timeout | The timeout has expired |
例外
任何由时钟、时间引发的异常[医]在执行%28时钟、时间点和标准库提供的持续时间期间,不要抛出%29。
注记
鼓励实现在下列情况下检测情况:valid == false
在呼叫前抛出一个future_error
错误条件为future_errc::no_state
...
钟系在timeout_time
使用,这不需要是一个单调的时钟。如果时钟不连续地调整,则不能保证此函数的行为,但是现有的实现转换。timeout_time
从Clock
到std::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!
二次
另见
wait | waits for the result to become available (public member function) |
---|---|
wait_for | waits for the result, returns if it is not available for the specified timeout duration (public member function) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。