Future
The last part of the C++11 thread support API is defined in <future>
. It offers a range of classes, which implement more high-level multithreading concepts aimed more at easy asynchronous processing rather than the implementation of a multithreaded architecture.
Here we have to distinguish two concepts: that of a future and that of a promise. The former is the end result (the future product) that'll be used by a reader/consumer. The latter is what the writer/producer uses.
A basic example of a future would be:
#include <iostream> #include <future> #include <chrono> bool is_prime (int x) { for (int i = 2; i < x; ++i) if (x%i==0) return false; return true; } int main () { std::future<bool> fut = std::async (is_prime, 444444443); 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...