Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to join thread if joinable do nothing otherwise?

Tags:

c++

boost

I'm using boost::thread join method to wait until thread is finished. But if I try to join the thread when it already finished I receive an exception. So how can I:

Join thread if active and do nothing if not active?

like image 733
Oleg Vazhnev Avatar asked Apr 15 '13 13:04

Oleg Vazhnev


1 Answers

Use the joinable() function to check whether it should be joined:

if (thread.joinable()) thread.join();

You can (in fact, you must) join a thread even if it's already finished, unless it's been detached. It's an error to join it twice, or to join an empty or detached thread.

like image 108
Mike Seymour Avatar answered Sep 28 '22 19:09

Mike Seymour