Boost logo

Boost Users :

Subject: [Boost-users] [Boost.Thread] Join not waiting?
From: James Auger (james.auger_at_[hidden])
Date: 2009-06-10 16:24:16


This is my first time working with Boost.Thread and I'm having some
difficulty in that Join does not seem to work (obviously I'm doing something
wrong).
In a nutshell what I'm doing is:
   Instantiating the Example class (which has a boost::thread as a member
variable)
   calling Example::run() (which starts the thread)
   do some stuff (both in main and in the thread)
   calling Example::terminate() (which sets the condition for the thread
function to stop, and calls Join)
   end.

The problem is that Join returns instantly, before the thread has finished.
 In the code below I've commented out the stop condition (making the thread
run an infinite loop) to demonstrate that it keeps running after join
returns. By my understanding of how boost threads and join works, this is
not the correct behavior; my understanding is that the call to join should
not return until the thread has stopped running. Either my understanding is
wrong, or my code to make that happen is wrong. Can you guys help me?

The example is stripped down/simplified code that I wrote to demonstrate and
test what was going on in my actual program.

Example Output:

---
thread: main: 00
thread: main: 11
thread: main: 22
thread: main: 33
main: thread: 44
main: After Terminate
thread: 5
thread: 6
thread: 7
thread: 8
---
Note the interleaved output 0 - 4, that's expected and fine.  The problem is
the thread is still running (as evidenced by writing to cout) after
terminate() (and thus join()) has been called on that thread.
Example Code:
---
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
class Example
{
public:
     Example()
     {
          flag = 0;
     };
     void run()
     {
          flag = 1;
          boost::thread _myThread(boost::bind(&Example::threadLoop, this));
     };
     void terminate()
     {
          //flag = 3;     //commented out stop condition
          _myThread.join();
     };
private:
     void threadLoop()
     {
          flag = 2;
          for(int i = 0; flag == 2; i++)
          {
               boost::this_thread::sleep(boost::posix_time::seconds(1));
               std::cout<<"thread: "<<i<<std::endl;
          }
          flag = 4;
     };
     boost::thread _myThread;
     int flag;
};
int main()
{
     Example anExample;
     anExample.run();
     for(int i = 0; i < 5; i++)
     {
          boost::this_thread::sleep(boost::posix_time::seconds(1));
          std::cout<<"main: "<<i<<std::endl;
     }
     anExample.terminate();
     std::cout<<"main: After Terminate"<<std::endl;
     boost::this_thread::sleep(boost::posix_time::seconds(5));  //sleep so
we can see the thread still outputing
     return 0;
}
---
Thanks!


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