On 21/05/2015 12:12, Lane wrote:
Here's a bit more detail of my current problem.
I have implemented destructors within ClassA and ClassB. When adding this thread to the thread_group, the thread objects destructors are being called as soon as they're added to the thread_group.
struct MyStruct : ... { boost::thread_group threads; };
template
void psm_::MyEvent::on_entry(Event const& ev, FSM& sm) { auto bb = std::make_shared (100); ClassA
c_a(bb); ClassB c_b(bb); threads.add_thread(new boost::thread(c_a); // destructors called here threads.add_thread(new boost::thread(c_b); }
No, what's happening there is that the c_a and c_b temporary objects are being *copied* to the thread, and then the *originals* are being destroyed. The copies owned by the thread remain undestroyed.
I've wrapped the thread_group within unique_ptr so that I can call reset, but I don't even make it to this point because the thread objects have already been destroyed when being added to the thread (and program exits).
template
void psm_::MyEvent::on_exit(Event const& ev, FSM& sm) { std::unique_ptrboost::thread_group tg(&threads); tg.reset(); threads.interrupt_all(); threads.join_all(); }
I'm sorry, but you clearly need to go back to school and learn how pointers work. (And then go back and re-read my earlier post. It's the one in MyStruct that has to be a unique_ptr and you're doing it in the wrong order.)