How to check if thread has finished work in C++11 and above?

左心房为你撑大大i 提交于 2019-12-04 09:55:58

You may want to use std::future, it provides higher level facilities where you can trivially check if the asynchronous computation is finished (aka ready): Example:

void mythread() {
    //do some stuff
}

template<typename T>
bool future_is_ready(std::future<T>& t){
    return t.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
}

int main() 
{
    std::future<void> foo = std::async(std::launch::async, mythread);  
    if (future_is_ready(foo)){
        //do some next stuff
    }
}

On the other hand, you may think simply using a "safe" (or atomic) flag works:

#include <iostream>
#include <thread>

std::atomic<bool> is_done{false};

void mythread() 
{
    //do some stuff
    ......
    is_done = true;
}
int main() 
{
  std::thread foo(mythread);  
  if (is_done)
  {
    foo.join();
    //do some next stuff
  }
  .....
  if(foo.joinable()) foo.join();
}

But, it doesn't work. While you think is_done = true is the last thing you did in mythread(); You may have created some objects of automatic storage duration in that scope, and since such objects are destroyed in the reverse order of construction, there will still be "some work" in that thread after setting is_done.

You want a future. Start your thread withstd::async and use wait_for with zero seconds on it. Compare the result against future_status::ready.

You can use wait_for of std::future to check whether the result is already there. A simple way to get a future for an asynchronous task is std::async.

#include <future>

// ...

// launch task and get result future
auto rf = std::async(std::launch::async, mythread);
// get initial status
auto status = rf.wait_for(std::chrono::nanoseconds(1));
// loop while task is not ready
while(status != std::future_status::ready)
{ 

    // not ready yet, do other stuff here

    // 1 nanosecond timeout check
    status = rf.wait_for(std::chrono::nanoseconds(1));
}
// we are done...
Rene

I had the same problem, and my solution was to wrap the thread class, so I can set a flag when the function has finished its work.

Here you find the discussion for the solution Stack Overflow

And here is the working example: Celma managed thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!