#include <future>
#include <iostream>
void main()
{
std::async(std::launch::async,[] {std::cout << "async..." << std::endl; while (1);});
std::cout << "runing main..." << std::endl;
}
In this code, only "async..." will be outputted, which means the code is blocked at async. However, if I add future and let the statement become:
std::future<bool> fut = std::async([]
{std::cout << "async..." << std::endl; while (1); return false; });
Then everything runs smoothly (it will not be blocked). I am not sure why it happen in this way. I think async is supposed to run in a separate thread.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…