Michael Hagar
Michael Hagar

Reputation: 646

How to specify function signature for function that returns result of async?

I have a function that does this:

??? createThread(int x) {
   return async(std::launch::async, 
      [x] () { // do stuff with x });
}

I am using VS 2012 (partial C++11). What should the return type be to make this compile?

Upvotes: 0

Views: 68

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 476940

Your function returns a std::future<void>: the future result of the call of the asynchronous function.

However, it is very poor practice to return a future returned from a call of std::async with the std::launch::async policy, as such a future's destructor may block, and users of the standard library usually don't expect standard library destructors to block. Cf. [futures.async]/4:

[Note: If a future obtained from std::async is moved outside the local scope, other code that uses the future must be aware that the future’s destructor may block for the shared state to become ready. — end note]

Upvotes: 2

Related Questions