wiki:hpp-future
<future>
<future> provides std::future<T> and std::promise<T> for passing values or exceptions between threads. std::async launches a function on a thread pool (or synchronously, depending on the policy) and returns a future that you wait on for the result.
It's the standard way to express “run this on another thread and get the result later.”
Example
This example launches an asynchronous task that sleeps and returns a value, then retrieves the result by calling get() on the future.
// compile: g++ -std=c++11 -pthread -o futureexample futureexample.cpp // run: ./futureexample // description: async task with future #include <future> #include <iostream> #include <thread> int slow_computation() { std::this_thread::sleep_for(std::chrono::milliseconds(100)); return 42; } int main() { auto fut = std::async(std::launch::async, slow_computation); std::cout << "waiting for result...\n"; int result = fut.get(); std::cout << "got: " << result << "\n"; return 0; }
wiki/hpp-future.md · Last modified: by 127.0.0.1
