[Boost.Thread] Join not waiting?

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

AMDG James Auger wrote:
<snip>
class Example { public: Example(); void run() { flag = 1; boost::thread _myThread(boost::bind(&Example::threadLoop, this)); };
You are creating a new boost::thread which is completely unrelated to the member _myThread. Try _myThread = boost::thread(boost::bind(&Example::threadLoop, this));
void terminate();
private: void threadLoop();
boost::thread _myThread; int flag; };
In Christ, Steven Watanabe

On Wed, Jun 10, 2009 at 4:44 PM, Steven Watanabe
AMDG
James Auger wrote:
<snip>
class Example { public: Example(); void run() { flag = 1; boost::thread _myThread(boost::bind(&Example::threadLoop, this)); };
You are creating a new boost::thread which is completely unrelated to the member _myThread. Try _myThread = boost::thread(boost::bind(&Example::threadLoop, this));
Thanks! That was the problem. Though I had to bang my head against the wall of boost syntax to figure out that it was looking for: _myThread = boost::move(boost::thread(boost::bind(&Example::threadLoop, this))); As much as I like boost, sometimes I feel like programing with it is like playing one of those text-based adventure games where the challenge isn't solving the puzzles, it's guessing the syntax...
void terminate();
private: void threadLoop();
boost::thread _myThread; int flag; };
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Thanks, James

As much as I like boost, sometimes I feel like programing with it is like playing one of those text-based adventure games where the challenge isn't solving the puzzles, it's guessing the syntax...
Objects' scope, locals vs. members - all these are just basic c++ rules, unrelated to boost or any other library.
participants (3)
-
Igor R
-
James Auger
-
Steven Watanabe