1

What is the difference in the following:

std::async(my_function);

and

std::async(std::launch::async, my_function);

What is the difference in using the pilicy std::launch::async in this case?? Does the first option not launch the function asynchronously anyway??

T.C.
  • 133,968
  • 17
  • 288
  • 421
Harry Boy
  • 4,159
  • 17
  • 71
  • 122

1 Answers1

3

The first one is equivalent to passing launch::async | launch::deferred, in which case it is up to the implementation whether it is launched asynchronously or merely deferred (called when a non-timed waiting function like get() is called on the returned future).

The idea is that by default, the implementation can choose to defer if creating a new thread isn't going to be a performance gain. To force a new thread to be created, pass launch::async only.

T.C.
  • 133,968
  • 17
  • 288
  • 421
  • 2
    It seems to me that `launch::async` only is the preferred approach in the vast majority of use cases. And that this is likely to be a frequently-found bug in user code :( – Lightness Races in Orbit Feb 22 '15 at 15:35
  • When you say that 'this is likely to be a frequently-found bug in user code' do you mean that the omission of 'launch::async' is the bug?? – Harry Boy Feb 23 '15 at 10:42