Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

c++ - async and promise - is this correct usage?

I am learning multithreading and I have found one example which bothers me. (I am not the author of this code).

#include <iostream>
#include <future>
using namespace std;

void testFunc(std::promise<int> &result, std::promise<bool> &done)
{
    // other... 

    result.set_value(10);           
    done.set_value(true); 

    // other.... 
}

int main()
{

    std::promise<int>       resultPromise;
    std::promise<bool>      donePromise;

    std::future<int>    resultFuture = resultPromise.get_future();
    std::future<bool>   doneFuture = donePromise.get_future();

    std::async(std::launch::async, testFunc,
               std::ref(resultPromise), std::ref(donePromise) );    // line A

    bool done = doneFuture.get();                   // line B
    int result_testFunc = resultFuture.get();       // line C

    // do other things with result_testFunc

    return 0;
}

It seems to me that it would be easier to use future which is returned by async. We can return value in testFunc() and then use get() on mentioned future. We don't need to create resultPromise and donePromise.

  1. Does above snippet presents proper usage of future and promise? (in theory)
  2. Will get(), called on line B, block until the thread function testFunc() has completed and exited?
question from:https://stackoverflow.com/questions/65869539/async-and-promise-is-this-correct-usage

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For this example, it would certainly make more sense to return a value in the lambda provided to std::async.

In general, futures and promises are meant to facilitate asynchronous programming. Where waiting should typically be avoided or at least reduced.

To avoid waiting altogether, you could use the experimental std::future::then to assign a continuation function that will process the result of the first lambda.

Alternatively you could use co_await which basically wraps std::future::then behavior in a more readable syntax. But in my opinion it is better if you first have a solid understanding of the std::future::then mechanism.

To reduce waiting, you can utilize the posting thread (main) to do something useful while the asynchronous task is running in the background.

Note that co_await is quite new to C++ and supported only on certain compilers.
std::future::then is still in experimental stages, but available at least on msvc.

As for your specific questions:

Does above snippet presents proper usage of future and promise? (in theory)

The code will work, but the usage pattern is not ideal as explained.

Will get(), called on line B, block until the thread function testFunc() has completed and exited?

Yes. It will block until testFunc has completed its work on a background thread.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...