Uninterupted output of destructors of ros-nodes [closed]

asked 2019-08-14 10:16:24 -0500

max11gen gravatar image

I'm having a ros system consiting of different nodes. Each of these nodes has a calback-class that does all the calculations. I define multiple std::couts in the destructors of the classes so that I can get some statistics output of what happened during the execution. This was working fine so far, because I only had one such DTOR defined. But now I thought it might be a good idea to have such statistics for each of my nodes and therefor I added those DTOR to all callback-classes. Now it frequently happens that one DTOR outputs it's first couple of lines, then another DTOR outputs a few lines and then the first DTOR again. Is there a way of making sure, that the output of each DTOR will not be interupted by the output of other DTORs?

edit retag flag offensive reopen merge delete

Closed for the following reason question is off-topic or not relevant. Please see http://wiki.ros.org/Support for more details. by gvdhoorn
close date 2019-08-15 03:50:25.393720

Comments

I understand that you are doing this in a ROS context/application, but your question is actually not ROS-specific. It seems to be about the order in which destructors are called and how the system handles thread/process scheduling. That makes it a question off-topic for this forum.

I would suggest you ask this on a more appropriate forum, such as one about C++ programming.

gvdhoorn gravatar imagegvdhoorn ( 2019-08-15 03:51:58 -0500 )edit

@gvdhoorn I see what you are saying, but I feel like it's a problem exclusive to the way that ROS is handling multiple nodes. Seems to me, like ROS is using a different thread for each node. I used to think, that it's only going to make benefit of multithreading when you specifically declare async spinners.

max11gen gravatar imagemax11gen ( 2019-08-15 03:58:29 -0500 )edit

Nodes are mapped onto processes. Nodelets use threads.

I may have misunderstood you though: are you using actual object instances for each callback (or multiple callbacks)?

gvdhoorn gravatar imagegvdhoorn ( 2019-08-15 04:03:23 -0500 )edit

I am using multiple nodes each of which has a main() function which will create an instance of a class (different class for each node) which will trigger multiple callbacks.
What are nodelets though, and what's the difference between nodelets and nodes?

max11gen gravatar imagemax11gen ( 2019-08-15 04:11:44 -0500 )edit

So you basically have something like:

main()
{
  MyRosNode node;
  ros::spin();
}

where MyRosNode creates a nr of Subscribers and registers class methods as callbacks?

And each of the MyRosNode classes has a dtor that has a nr of std::couts in them?

gvdhoorn gravatar imagegvdhoorn ( 2019-08-15 04:15:08 -0500 )edit

@gvdhoorn Exactly. Sorry for not making that more clear. Sometimes I'm a little puzzled about what is the clearest way to make people understand, what I'm doing.
In case that matters: Before MyRosNode node; I will also do

ros::init(argc,argv, "MyRosNode");
ros::NodeHandle n;
max11gen gravatar imagemax11gen ( 2019-08-15 04:19:59 -0500 )edit

And you have multiple nodes that follow this structure?

In that case your node is just a process. Not a thread.

Callbacks could be handled multithreaded, but only if you'd create a multi-threaded or async spinner.

But that will only influence the way callback code is executed. Not the dtors afaik.

gvdhoorn gravatar imagegvdhoorn ( 2019-08-15 04:31:43 -0500 )edit

@gvdhoorn Yes, I currently have three nodes following this structure. But does that mean the only thing that I would have to change in my code, to profit from multiple CPU cores and multithreading is to create a multi-threaded or async spinner in each node in the main function?

max11gen gravatar imagemax11gen ( 2019-08-15 04:53:19 -0500 )edit