[关闭]
@zhengyuhong 2015-06-08T07:24:04.000000Z 字数 3681 阅读 1166

future

C++11 STL


future

  future 对象可以异步返回共享状态的值,或者在必要的情况下阻塞调用者并等待共享状态标志变为 ready,然后才能获取共享状态的值。

future::future

  1. future() noexcept;
  2. future (const future&) = delete;
  3. future (future&& x) noexcept;
  1. // future::future
  2. #include <iostream> // std::cout
  3. #include <future> // std::async, std::future
  4. int get_value() { return 10; }
  5. int main ()
  6. {
  7. std::future<int> foo; // default-constructed
  8. std::future<int> bar = std::async (get_value); // move-constructed
  9. int x = bar.get();
  10. std::cout << "value: " << x << '\n';
  11. return 0;
  12. }

future::get

  1. T get();
  2. R& future<R&>::get(); // when T is a reference type (R&)
  3. void future<void>::get(); // when T is void

example

  1. // future::get
  2. #include <iostream> // std::cout, std::ios
  3. #include <future> // std::async, std::future
  4. #include <exception> // std::exception
  5. int get_int() {
  6. std::cin.exceptions (std::ios::failbit); // throw on failbit set
  7. int x;
  8. std::cin >> x; // sets failbit if invalid
  9. return x;
  10. }
  11. int main ()
  12. {
  13. std::future<int> fut = std::async (get_int);
  14. std::cout << "Please, enter an integer value: ";
  15. try {
  16. int x = fut.get();
  17. std::cout << "You entered: " << x << '\n';
  18. }
  19. catch (std::exception&) {
  20. std::cout << "[exception caught]";
  21. }
  22. return 0;
  23. }

future::valid

  1. bool valid() const noexcept;
  1. #include <iostream> // std::cout
  2. #include <future> // std::async, std::future
  3. #include <utility> // std::move
  4. int get_value() { return 10; }
  5. int main ()
  6. {
  7. std::future<int> foo,bar;
  8. foo = std::async (get_value);
  9. bar = std::move(foo);
  10. if (foo.valid())
  11. std::cout << "foo's value: " << foo.get() << '\n';
  12. else
  13. std::cout << "foo is not valid\n";
  14. if (bar.valid())
  15. std::cout << "bar's value: " << bar.get() << '\n';
  16. else
  17. std::cout << "bar is not valid\n";
  18. return 0;
  19. }

future::wait

  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

  1. // future::wait
  2. #include <iostream> // std::cout
  3. #include <future> // std::async, std::future
  4. #include <chrono> // std::chrono::milliseconds
  5. // a non-optimized way of checking for prime numbers:
  6. bool is_prime (int x) {
  7. for (int i=2; i<x; ++i) if (x%i==0) return false;
  8. return true;
  9. }
  10. int main ()
  11. {
  12. // call function asynchronously:
  13. std::future<bool> fut = std::async (is_prime,194232491);
  14. std::cout << "checking...\n";
  15. fut.wait();
  16. std::cout << "\n194232491 ";
  17. if (fut.get()) // guaranteed to be ready (and not block) after wait returns
  18. std::cout << "is prime.\n";
  19. else
  20. std::cout << "is not prime.\n";
  21. return 0;
  22. }

future::wait_for

  1. template <class Rep, class Period>
  2. future_status wait_for (const chrono::duration<Rep,Period>& rel_time) const;

const chrono::duration参考thread

future::wait_until

  1. template <class Clock, class Duration>
  2. future_status wait_until (const chrono::time_point<Clock,Duration>& abs_time) const;

const chrono::time_point参考thread

async

异步执行

  1. template <class Fn, class... Args>
  2. future<typename result_of<Fn(Args...)>::type> async (Fn&& fn, Args&&... args);
  3. template <class Fn, class... Args>
  4. 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.

  1. // async example
  2. #include <iostream> // std::cout
  3. #include <future> // std::async, std::future
  4. // a non-optimized way of checking for prime numbers:
  5. bool is_prime (int x) {
  6. std::cout << "Calculating. Please, wait...\n";
  7. for (int i=2; i<x; ++i) if (x%i==0) return false;
  8. return true;
  9. }
  10. int main ()
  11. {
  12. // call is_prime(313222313) asynchronously:
  13. std::future<bool> fut = std::async (is_prime,313222313);
  14. std::cout << "Checking whether 313222313 is prime.\n";
  15. bool ret = fut.get(); // waits for is_prime to return
  16. if (ret)
  17. std::cout << "It is prime!\n";
  18. else
  19. std::cout << "It is not prime.\n";
  20. return 0;
  21. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注