main thread waits for std::async to complete [duplicate]

亡梦爱人 提交于 2019-11-28 13:59:08

A std::future object returned by std::async and launched with std::launch::async policy, blocks on destruction until the task that was launched has completed.

Since you do not store the returned std::future in a variable, it is destroyed at the end of the statement with std::async and as such, main cannot continue until the task is done.

If you store the std::future object, its lifetime will be extended to the end of main and you get the behavior you want.

int main()
{
    ...
    auto fut = std::async(std::launch::async, fun, nullptr);
    ...
}
std::async(std::launch::async, fun, nullptr);

Doesn't do anything with the returned std::future, leaving it to be destroyed. That's a problem because std::future's destructor may block and wait for the thread to finish.

The solution is to hold on to the std::future for a while and let it be destroyed after you're done with everything else.

auto locallyScopedVariable = std::async(std::launch::async, fun, nullptr);

locallyScopedVariable will go out of scope at the end of main and then block until it completes.

Note that this still might not do quite what you want. The main thread could immediately yield the processor to the new thread and allow the new thread to run to completion before control is returned. The code can be corrected and still result in the output of the incorrect version.

(1) In multi-threading program testing, protect shared resource (cout in this case) from being invoked from different threads at same time using a mutex. (2) Check if future is ready in the main, you can do a timeout also.

void print_id() 
{
    lock_guard<mutex> locker(mutex_);
    cout << "Thread id is:" << this_thread::get_id() << endl;
}

void print( string str) 
{
    lock_guard<mutex> locker(mutex_);
    cout << str << '\n';
}   

bool fun(void *obj)
{
   print("Entry");
   printid();
   Sleep(10000);
   print("Exit");
   return true;
}


int main()
{
    print("Hello");
    printid();
    std::future<bool> fut = std::async(std::launch::async, fun,nullptr);
    while(!fut->_Is_ready() )
    {
     }
    cout << "After call" << endl;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!