Boost logo

Boost Users :

Subject: Re: [Boost-users] How to call destructor on thread object?
From: Lane (software.research.development_at_[hidden])
Date: 2015-05-20 20:12:03


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);
    }

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();
    }

And if I don't add the destructors to the thread objects (ClassA, ClassB),
I am able to reach on_exit when I need to.

Any thoughts as to why the destructors (which I need to make use of in
on_exit) are being called when adding the new threads?

On Wed, May 20, 2015 at 1:47 PM, Lane <
software.research.development_at_[hidden]> wrote:

> I know you did, and thats still one path I'm following, but I'm having a
> hard time trying to understand how to wrap it in the way I currently have
> it written. If you have the time, please post.
>
> On Tue, May 19, 2015 at 7:29 PM, Gavin Lambert <gavinl_at_[hidden]>
> wrote:
>
>> On 20/05/2015 08:40, Lane wrote:
>>
>>> Thanks for the explanation, that helps. But what I'm looking to do is
>>> find a way to call the destructor on the thread object. The ClassA and
>>> classB objects are connected to a USB device and I need to find a way to
>>> properly shut them down before destroying the threads in on_exit(). Any
>>> thoughts?
>>>
>>
>> I covered that in my second reply. After joining the threads, you have
>> to destroy the thread_group. But first you must make it destroyable, by
>> putting it in a unique_ptr.
>>
>>
>>
>> _______________________________________________
>> Boost-users mailing list
>> Boost-users_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>>
>
>



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