
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

Alexander Heinrich <alexander.tobias.heinrich@googlemail.com> writes:
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.
That is true.
So what should I do instead if I want to know if a thread is still running?
Set a flag at the end of the thread function. Then, in other code, you can check the flag. If the flag is set then the thread is done. Anthony -- Author of C++ Concurrency in Action | http://www.manning.com/williams just::thread C++0x thread library | http://www.stdthread.co.uk Just Software Solutions Ltd | http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
participants (2)
-
Alexander Heinrich
-
Anthony Williams