@zhengyuhong
2015-06-08T07:24:04.000000Z
字数 3681
阅读 1166
C++11 STL
future 对象可以异步返回共享状态的值,或者在必要的情况下阻塞调用者并等待共享状态标志变为 ready,然后才能获取共享状态的值。
future() noexcept;future (const future&) = delete;future (future&& x) noexcept;
// future::future#include <iostream> // std::cout#include <future> // std::async, std::futureint get_value() { return 10; }int main (){std::future<int> foo; // default-constructedstd::future<int> bar = std::async (get_value); // move-constructedint x = bar.get();std::cout << "value: " << x << '\n';return 0;}
T get();R& future<R&>::get(); // when T is a reference type (R&)void future<void>::get(); // when T is void
example
// future::get#include <iostream> // std::cout, std::ios#include <future> // std::async, std::future#include <exception> // std::exceptionint get_int() {std::cin.exceptions (std::ios::failbit); // throw on failbit setint x;std::cin >> x; // sets failbit if invalidreturn x;}int main (){std::future<int> fut = std::async (get_int);std::cout << "Please, enter an integer value: ";try {int x = fut.get();std::cout << "You entered: " << x << '\n';}catch (std::exception&) {std::cout << "[exception caught]";}return 0;}
bool valid() const noexcept;
#include <iostream> // std::cout#include <future> // std::async, std::future#include <utility> // std::moveint get_value() { return 10; }int main (){std::future<int> foo,bar;foo = std::async (get_value);bar = std::move(foo);if (foo.valid())std::cout << "foo's value: " << foo.get() << '\n';elsestd::cout << "foo is not valid\n";if (bar.valid())std::cout << "bar's value: " << bar.get() << '\n';elsestd::cout << "bar is not valid\n";return 0;}
Waits for the shared state to be ready.
If the shared state is not yet ready (i.e., the provider has not yet set its value or exception), the function blocks the calling thread and waits until it is ready.
Once the shared state is ready, the function unblocks and returns without reading its value nor throwing its set exception (if any).
example
// future::wait#include <iostream> // std::cout#include <future> // std::async, std::future#include <chrono> // std::chrono::milliseconds// a non-optimized way of checking for prime numbers:bool is_prime (int x) {for (int i=2; i<x; ++i) if (x%i==0) return false;return true;}int main (){// call function asynchronously:std::future<bool> fut = std::async (is_prime,194232491);std::cout << "checking...\n";fut.wait();std::cout << "\n194232491 ";if (fut.get()) // guaranteed to be ready (and not block) after wait returnsstd::cout << "is prime.\n";elsestd::cout << "is not prime.\n";return 0;}
template <class Rep, class Period>future_status wait_for (const chrono::duration<Rep,Period>& rel_time) const;
const chrono::duration参考thread
template <class Clock, class Duration>future_status wait_until (const chrono::time_point<Clock,Duration>& abs_time) const;
const chrono::time_point参考thread
异步执行
template <class Fn, class... Args>future<typename result_of<Fn(Args...)>::type> async (Fn&& fn, Args&&... args);template <class Fn, class... Args>future<typename result_of<Fn(Args...)>::type> async (launch policy, Fn&& fn, Args&&... args);
Call function asynchronously
Calls fn (with args as arguments) at some point, returning without waiting for the execution of fn to complete.
The value returned by fn can be accessed through the future object returned (by calling its member future::get).
The second version (2) lets the caller select a specific launching policy, while the first version (1) uses automatic selection, as if calling (2) with launch::async|launch::deferred as policy.
// async example#include <iostream> // std::cout#include <future> // std::async, std::future// a non-optimized way of checking for prime numbers:bool is_prime (int x) {std::cout << "Calculating. Please, wait...\n";for (int i=2; i<x; ++i) if (x%i==0) return false;return true;}int main (){// call is_prime(313222313) asynchronously:std::future<bool> fut = std::async (is_prime,313222313);std::cout << "Checking whether 313222313 is prime.\n";bool ret = fut.get(); // waits for is_prime to returnif (ret)std::cout << "It is prime!\n";elsestd::cout << "It is not prime.\n";return 0;}