samedi 22 avril 2017

Task executed with std::async is blocking like if future was used

I am having a hard time understanding why following code blocks:

  {
    std::async(std::launch::async, [] { std::this_thread::sleep_for(5s); 
    // this line will not execute until above task finishes?
  }

I suspect that std::async returns std::future as temporary which in destructor joins on the task thread. Is it possible?

Full code is below:

int main() {
  using namespace std::literals;

  {
    auto fut1 = std::async(std::launch::async, [] { std::this_thread::sleep_for(5s); std::cout << "work done 1!\n"; });
    // here fut1 in its destructor will force a join on a thread associated with above task.
  }
  std::cout << "Work done - implicit join on fut1 associated thread just ended\n\n";

    std::cout << "Test 2 start" << std::endl;
  {
    std::async(std::launch::async, [] { std::this_thread::sleep_for(5s); std::cout << "work done 2!" << std::endl; });
    // no future so it should not join - but - it does join somehow.
  }
  std::cout << "This shold show before work done 2!?" << std::endl;

}

Aucun commentaire:

Enregistrer un commentaire