how to join thread if joinable do nothing otherwise?

boost, c++

Solution

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.

Problem

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?

Original source

Related problems