My book is trying to explain the use of async() in multithreading.
The book says that launch::deferred as a parameter in async() runs function calculate() in the same thread, and that launch::async is run in a new thread by the time that fut.get() is called.
Can someone explain this in a simpler way or in different words? I don't see which cases I would need to use one or the other...
#include <iostream>
#include <future>
usingnamespace std;
int calculate()
{
return 123;
}
int main()
{
auto fut = async(calculate);
//auto fut = async(launch::async, calculate);
//auto fut = async(launch::deferred, calculate);
// Do some more work...
// Get result
int res = fut.get();
cout << res << endl;
return 0;
}