#include#include using namespace std; int main() { promise prom; future fut = prom.get_future(); thread th([](promise p){ p.set_value(10); }, move(prom)); int result = fut.get(); cout << "Result: " << result << endl; th.join(); return 0; }
#includeThis example uses the async function to create two parallel computations that return values. Promises are automatically created and Futures obtained from the async function. Finally, the results are retrieved and printed. The package library for C++ Promise is likely going to be the standard C++ library which is included by default in most C++ compilers.#include using namespace std; int main() { auto p1 = async([](){ return 42; }); auto p2 = async([](){ return 100; }); auto fut1 = p1.get_future(); auto fut2 = p2.get_future(); int result1 = fut1.get(); int result2 = fut2.get(); cout << "Result1: " << result1 << endl; cout << "Result2: " << result2 << endl; cout << "Sum: " << result1 + result2 << endl; return 0; }