Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...