|
Boost Users : |
Subject: Re: [Boost-users] How to call destructor on thread object?
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2015-05-20 20:23:42
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 <class Event, class FSM>
> void psm_::MyEvent::on_entry(Event const& ev, FSM& sm)
> {
> auto bb = std::make_shared<bounded_buffer<MyData>>(100);
>
> ClassA<bounded_buffer<MyData>> c_a(bb);
> ClassB<bounded_buffer<MyData>> 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 <class Event, class FSM>
> void psm_::MyEvent::on_exit(Event const& ev, FSM& sm)
> {
> std::unique_ptr<boost::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.)
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