Boost logo

Boost Users :

Subject: [Boost-users] How to call destructor on thread object?
From: Lane (software.research.development_at_[hidden])
Date: 2015-05-18 23:26:04


I'm using the boost meta state machine and upon entry of this state, I
create two threads, and stop these threads in the on_exit().

In on_entry(), I call the functors of each class and create threads and add
those to a boost thread_group. What I need to do now is call the destructor
of the objects on the threads ( of ClassA and ClassB) within on_exit(). I'm
unclear as to how the destructors of the objects would/should be called
when interrupting the threads. The destructors are currently not being
called.

    struct MyEvent : public msm::front::state<>
    {
        template <class Event, class FSM>
        void on_entry(Event const&, FSM&);

        template <class Event, class FSM>
        void on_exit(Event const&, FSM&);

        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));
        threads.add_thread(new boost::thread(c_b));
    }

    template <class Event, class FSM>
    void psm_::MyEvent::on_exit(Event const& ev, FSM& sm)
    {
        threads.interrupt_all();
        threads.join_all();
    }

    template <class Buffer>
    class ClassB
    {
        private:
            typedef typename Buffer::value_type value_type;

            shared_ptr<Buffer> m_container;

        public:
            ClassB(shared_ptr<Buffer> buffer) : m_container(buffer) { }
           ~ClassB() { // ... }

        void operator()() {
                try {
                    // ... do stuff
                    boost::this_thread::interruption_point();
                }
                catch(const boost::thread_interrupted&) {
                    // ...
                }
            }
    }



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