jeudi 23 février 2017

How to check if thread has finished work in C++ >= 11?

As the question how to check if thread has finished work in C++ >= 11. I have been reading the documentation and I have read something like below:

#include <iostream>
#include <thread>
void mythread() 
{
    //do some stuff
}
int main() 
{
  std::thread foo(mythread);  
  if (foo.joinable())
  {
    foo.join();
    //do some next stuff
  }
}

Method joinable tells only that thread has started work, but I would like to know how to write a code to check if thread has finished work.

So I would like to write a code something like below:

#include <iostream>
#include <thread>
void mythread() 
{
    //do some stuff
}
int main() 
{
  std::thread foo(mythread);  
  if (foo.finishedWork())
  {
    foo.join();
    //do some next stuff
  }
}

There is something like futures and promises, but I don't know how to use it in this code.

Aucun commentaire:

Enregistrer un commentaire