samedi 23 avril 2016

Why C++ async run sequentially without future?

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

Aucun commentaire:

Enregistrer un commentaire