Boost logo

Boost Users :

Subject: [Boost-users] [thread]
From: Alexander Heinrich (alexander.tobias.heinrich_at_[hidden])
Date: 2009-09-10 09:03:43


I want to determine if a given thread is still running or has already
completed. I thought the joinable() method would provide this information,
but unfortunately it seems that a thread does not automatically become
Not-A-Thread after completion. I wrote a small app to verify this:

<code>
#include <iostream>
using namespace std;

#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
using namespace boost;
using namespace boost::posix_time;

mutex ioMutex;

void f()
{
    mutex::scoped_lock lock(ioMutex);
    cout << "done" << endl;
}

int main(int argc, char** argv)
{
    boost::thread t(f);

    while (t.joinable())
    {
        {
            mutex::scoped_lock lock(ioMutex);
            cout << "not yet done" << endl;
        }
        this_thread::sleep(seconds(2));
    }
    return 0;
}
</code>

Output:
not yet done
done
not yet done
not yet done
...

So what should I do instead if I want to know if a thread is still running?

Best regards,
Alexander Heinrich



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net