Retrieving Results from a Thread by Using a promise on One Thread - C++ Async

C++ examples for Async:promise

Description

Retrieving Results from a Thread by Using a promise on One Thread

Demo Code

#include <future>
#include <iostream>

using namespace std;

using FactorialPromise = promise< long long >;

long long Factorial(unsigned int value)
{
    return value == 1
        ? 1/*from  www . ja v  a 2s  .  com*/
        : value * Factorial(value - 1);
}

int main(int argc, char* argv[])
{
    using namespace chrono;

    FactorialPromise promise;
    future<long long> taskFuture{ promise.get_future() };

    promise.set_value(Factorial(3));
    cout << "Factorial result was " << taskFuture.get() << endl;

    return 0;
}

Result


Related Tutorials