
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