#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.
From encppreference.com
:
If the
std::future
obtained fromstd::async
is not moved from or bound to a reference, the destructor of thestd::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:
std::future
was created via std::async
;...then the call is blocking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With