ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Do I need to put any protections around the data so that they don't step on each other?

This is going to sound pedantic, but: how would you do this in a multi-threaded setup without any ROS involved?


Thing is: using ROS doesn't necessarily influence this: whenever there is a potential for conflicts (ie: lost updates, concurrent writes, etc) when accessing a shared resource from multiple threads of execution it would be prudent to protect access to that resource with appropriate measures (locks, mutexes, etc).

Adding ROS here doesn't necessarily change any of that: Subscribers and Publishers don't directly access any data structures internal to your class(es)/node(s): your message and/or timer callbacks do that. That is code you write, so you control it. Adding appropriate guards to avoid problems could be done there.

ROS 1 and ROS 2 -- at a high level -- offer similar threading models: either single or multi-threaded event processing (spinners in ROS 1, executors in ROS 2).

In ROS 1 however, if you write the node, you embed the choice you made on which threading model will be used in your code. Users of your node would have to change the code to change it (and at that point they become responsible for making sure everything keeps working correctly).

ROS 2 -- through composition -- allows users more control over node execution and scheduling. There is potential for your code being used in multi-threaded executors, or in single threaded ones. As such, you can't avoid thinking about thread-safety of access to shared resources I believe.


But I believe there is no real difference between writing something that is thread-safe for use without ROS or with it. In the end it's the same, and I believe the same techniques can be used to deal with it.