I was reading abut std::async with std::launch::async and read that with that policy, the callable will be called in a new thread.
So for sake of test, I did as follow:
struct Wrapper
{
void consume()
{
std::cout << "consume " << std::this_thread::get_id() << std::endl;
std::for_each(arr.begin(), arr.end(), [](int val) {std::cout << val; });
}
bool produce()
{
std::cout << "produce " << std::this_thread::get_id() << std::endl;
arr = { 1,0,3 };
return true;
}
};
int main()
{
std::cout << "main " << std::this_thread::get_id() << std::endl;
Wrapper wrap;
std::future<bool> fut = std::async(std::launch::async, &Wrapper::produce, &wrap);
if (fut.get())
std::async(std::launch::async, &Wrapper::consume, &wrap);
}
So based on that, I will excpect 3 threads :
- thread 1 : the main thread
- thread 2 : the first call of std::async (executing the produce fct)
- thread 3 : the second call of std::async (executing the consume fct)
when I run the program i got :
Why is the two calls of the std::async have the same thread ID ??
Thank you.

produceandconsumeare not executed concurrently.consumeis called whenproducefinished. So these functions can be executed by the same thread. Call your code concurrently and you see two different IDs. - rafix07