【C/C++】标准库之 future

Backto C/C++ Index


future 可以理解为金融中的 期货. 我有一个 future 类型的变量, 交给一个异步的线程去处理. 我到期来提货交割就可以了.这个能提供 future 服务的就叫provider.

Header with facilities that allow asynchronous access to values set by specific providers, possibly in a different thread.

Each of these providers (which are either promise or packaged_task objects, or calls to async) share access to a shared state with a future object: the point where the provider makes the shared state ready is synchronized with the point the future object accesses the shared state.

future 初探

// future example
#include        // std::cout
#include          // std::async, std::future
#include          // 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, 444444443);

	// do something while waiting for function to set future:
	std::cout << "checking, please wait";
	std::chrono::milliseconds span(100);
	while (fut.wait_for(span) == std::future_status::timeout)
		std::cout << '.' << std::flush;

	bool x = fut.get();     // retrieve return value

	std::cout << "\n444444443 " << (x ? "is" : "is not") << " prime.\n";

	return 0;
}

---
=> checking, please wait........
444444443 is prime.

这里很重要的一个函数就是 fut.get(), future 里的内容执行完了, 返回; 没有执行完, 就等待执行完, 再返回.

Returns the value stored in the shared state (or throws its exception) when the shared state is 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 (or throws) releasing its shared state. This makes the future object no longer valid: this member function shall be called once at most for every future shared state.
All visible side effects are synchronized between the point the provider makes the shared state ready and the return of this function.
The member of the void specialization does not return any value, but still waits for the shared state to become ready and releases it.

Promise: 专业的 future 提供商

// promise example
#include        // std::cout
#include      // std::ref
#include          // std::thread
#include          // std::promise, std::future

void print_int (std::future<int>& fut) {
  int x = fut.get();
  std::cout << "value: " << x << '\n';
}

int main ()
{
  std::promise<int> prom;                      // create promise

  std::future<int> fut = prom.get_future();    // engagement with future

  std::thread th1 (print_int, std::ref(fut));  // send future to new thread

  prom.set_value (10);                         // fulfill promise
                                               // (synchronizes with getting the future)
  th1.join();
  return 0;
}

例子中, 显示 create 了一个 返回int型future的 promise, 然后把这个promise 返回的futre 甩到一个新线程中去. 前期工作准备好后, 给 promise 中的int 量赋值, 然后让新线程 join 进来. 处理完毕, 交割完成. over.

Ref

  • http://www.cplusplus.com/reference/future/

你可能感兴趣的:(C/C++)