Hamed JML
Hamed JML

Reputation: 249

Check if thread is finished in c++11?

I need a way to know if the thread I am running is finished or not. If it is not finished it wait and if it is finished print a successful message.

I don't find a method or something like that in c++11 from library thread. I can't set a global variable because inside thread I am using execvp and it does not return if successful.

Is there any way to do that? A method or flag or anything else.

Edit: To make it clear I want to write a function that checks if thread is finished.

Upvotes: 1

Views: 5849

Answers (1)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136256

You can use C++11 futures and promises:

Futures are a high level mechanism for passing a value between threads, and allow a thread to wait for a result to be available without having to manage the locks directly.

The benefits of futures over using plain threads are:

  • Future returns a value.
  • You can wait on a future with a timeout.

Surely, this can be done with plain threads, but why reimplement a wheel with C++11.

Upvotes: 6

Related Questions