Robotics StackExchange | Archived questions

Uninterupted output of destructors of ros-nodes

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?

Asked by max11gen on 2019-08-14 10:16:24 UTC

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.

Asked by gvdhoorn on 2019-08-15 03:51:58 UTC

@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.

Asked by max11gen on 2019-08-15 03:58:29 UTC

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)?

Asked by gvdhoorn on 2019-08-15 04:03:23 UTC

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?

Asked by max11gen on 2019-08-15 04:11:44 UTC

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?

Asked by gvdhoorn on 2019-08-15 04:15:08 UTC

@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;

Asked by max11gen on 2019-08-15 04:19:59 UTC

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.

Asked by gvdhoorn on 2019-08-15 04:31:43 UTC

@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?

Asked by max11gen on 2019-08-15 04:53:19 UTC

Yes (sort of). There are some limitations/gotchas. There have been a few Q&As on this site about that.

You'll want to look into AsyncSpinner and/or MultithreadedSpinner.

I don't have the time right now to write a proper answer or link to the pages, sorry.

Asked by gvdhoorn on 2019-08-15 04:54:47 UTC

@gvdhoorn No worries, you already helped me a lot, even though not directly on the matter that I asked this question about. :D Thanks!

Asked by max11gen on 2019-08-15 08:01:06 UTC

Answers