skip to content
Ivan's wiki
Site Tools
Search
Tools
Show pagesource
Backlinks
Recent Changes
Media Manager
Sitemap
>
Recent Changes
Media Manager
Sitemap
hpp-future
This page is read only. You can view the source, but not change it.
# <future> **[<future>](https://en.cppreference.com/w/cpp/header/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." This example launches an asynchronous task that sleeps and returns a value, then retrieves the result by calling `get()` on the future. ```cpp // 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; } ```
Page Tools
Show pagesource
Backlinks
Back to top