Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 275
Ling He Avatar asked Apr 23 '16 21:04

Ling He


1 Answers

From encppreference.com:

If the std::future obtained from std::async is not moved from or bound to a reference, the destructor of the std::future will block at the end of the full expression until the asynchronous operation completes, essentially making code such as the following synchronous:

std::async(std::launch::async, []{ f(); }); // temporary's dtor waits for f()
std::async(std::launch::async, []{ g(); }); // does not start until f() completes

If I did get that right, it comes from these parts of the standard (N4527):

§30.6.6 [futures.unique_future]:

~future();

Effects:

— releases any shared state (30.6.4);

§30.6.4#5 [futures.state] (emphasis is mine):

When an asynchronous return object or an asynchronous provider is said to release its shared state, it means:

[...].

— these actions will not block for the shared state to become ready, except that it may block if all of the following are true: the shared state was created by a call to std::async, the shared state is not yet ready, and this was the last reference to the shared state.

Since you did not store the result of your first std::async call, the destructor of std::future is called and since all 3 conditions are met:

  • the std::future was created via std::async;
  • the shared state is not yet ready (due to your infinite loop);
  • there is no remaining reference to this future

...then the call is blocking.

like image 98
Holt Avatar answered Oct 08 '22 10:10

Holt