about std::async launch::deferred

I have question about std::async launch::deferred

does it launches the function with seperate thread and joins it as with launch::async ?
Last edited on
I guess that it's up to the implementation to decide but I see no reason why it would launch a second thread.
According to cppreference it does not launch a new thread. https://en.cppreference.com/w/cpp/thread/async

It's easy to test by checking the thread id.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <thread>
#include <future>

int main()
{
	auto main_thread_id = std::this_thread::get_id();
	auto async_thread_id = std::async(std::launch::deferred, []{ return std::this_thread::get_id(); }).get();
	
	if (main_thread_id == async_thread_id)
	{
		std::cout << "same thread\n";
	}
	else
	{
		std::cout << "different threads\n";
	}
}

When I test this with GCC 7.3 it prints "same thread".
Last edited on
Topic archived. No new replies allowed.