How to call destructor on thread object?
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
On 19/05/2015 15:26, Lane wrote:
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.
Since you are passing a copy of the object as the thread's state, the destructor for the local instances will be called in on_entry(), and the destructor for the instances owned by the thread will be called when the thread_group is destroyed, which is when MyEvent is destroyed. I'm not really familiar enough with Boost.MSM to know whether the event instance is destroyed when the event is handled (which if this were the case, you shouldn't be keeping threads in it), or when the containing state machine is destroyed, or only when the program exits.
Mere moments ago, quoth I:
On 19/05/2015 15:26, Lane wrote:
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.
Since you are passing a copy of the object as the thread's state, the destructor for the local instances will be called in on_entry(), and the destructor for the instances owned by the thread will be called when the thread_group is destroyed, which is when MyEvent is destroyed.
I'm not really familiar enough with Boost.MSM to know whether the event instance is destroyed when the event is handled (which if this were the case, you shouldn't be keeping threads in it), or when the containing state machine is destroyed, or only when the program exits.
Sorry, pressed Send too quickly. If you want to destroy the threads in on_exit instead, then since thread_group lacks a means to remove a thread (unless you still have a reference to it), you will have to destroy the thread_group instead. The simplest way to do that is to wrap it in a std::unique_ptr and reset() it in on_exit.
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?
On Tue, May 19, 2015 at 12:49 AM, Gavin Lambert
Mere moments ago, quoth I:
On 19/05/2015 15:26, Lane wrote:
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.
Since you are passing a copy of the object as the thread's state, the destructor for the local instances will be called in on_entry(), and the destructor for the instances owned by the thread will be called when the thread_group is destroyed, which is when MyEvent is destroyed.
I'm not really familiar enough with Boost.MSM to know whether the event instance is destroyed when the event is handled (which if this were the case, you shouldn't be keeping threads in it), or when the containing state machine is destroyed, or only when the program exits.
Sorry, pressed Send too quickly.
If you want to destroy the threads in on_exit instead, then since thread_group lacks a means to remove a thread (unless you still have a reference to it), you will have to destroy the thread_group instead.
The simplest way to do that is to wrap it in a std::unique_ptr and reset() it in on_exit.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Tue, May 19, 2015 at 4:40 PM, Lane
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?
It sounds like an obvious response, but, "when it goes out of scope"? So there must be some instance, reference, pointer (if dynamically allocated, shared pointer, etc), holding onto it?
On Tue, May 19, 2015 at 12:49 AM, Gavin Lambert
wrote: Mere moments ago, quoth I:
On 19/05/2015 15:26, Lane wrote:
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.
Since you are passing a copy of the object as the thread's state, the destructor for the local instances will be called in on_entry(), and the destructor for the instances owned by the thread will be called when the thread_group is destroyed, which is when MyEvent is destroyed.
I'm not really familiar enough with Boost.MSM to know whether the event instance is destroyed when the event is handled (which if this were the case, you shouldn't be keeping threads in it), or when the containing state machine is destroyed, or only when the program exits.
Sorry, pressed Send too quickly.
If you want to destroy the threads in on_exit instead, then since thread_group lacks a means to remove a thread (unless you still have a reference to it), you will have to destroy the thread_group instead.
The simplest way to do that is to wrap it in a std::unique_ptr and reset() it in on_exit.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
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.
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
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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
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
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
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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
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.)
participants (3)
-
Gavin Lambert
-
Lane
-
Michael Powell