multithreading and launch parameter

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...

example from the book...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <future>

using namespace 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;
}
Last edited on
Topic archived. No new replies allowed.