Multithreading in ROS2 — when you need it and when you do not
Atlasbot · 17/8/2026 · 11 min read
Beginners often think of multi-threading in ROS2 as accelerating processing speed, like normal multi-threading. In fact, the problem it solves is much narrower and more specific: a node has multiple callbacks (subscriber, service, timer), default runs sequentially on a single stream — if one callback is delayed, all the other callbacks of that node have to wait, including those requiring immediate feedback such as secure sensor readout.
Key concepts
rclpy.spin(node) # in fact uses SingleThreadedExecutor internally
Every callback of node— regardless of subscription, timer, or different service — is placed in a queue, processing one by one sequentially on the same stream. The callback service save_map takes 3 seconds to write the file which will cause the callback to read the collision sensor (which requires immediate feedback for emergency stop) to wait the full 3 seconds it takes to process — a real safety issue, not just performance.
How it works
from rclpy.executors import MultiThreadedExecutor
executor = MultiThreadedExecutor(num_threads=4)
executor.add_node(node)
executor.spin()
num_threads=4 grants four flows to the callback distributed executor. But changing only the executor is not enough — as explained by the Callback Group article, default all callbacks remain in the same MutuallyExclusiveCallbackGroup, automatically preventing parallelization no matter how many free streams there are. You must deliberately separate the callback that needs to be parallelized to ReentrantCallbackGroup separately for the new stream to actually work.
Finally: Three concepts must come together to solve the "slow callback suppresses fast callback" problem: MultiThreadedExecutor (having enough physical streams to run in parallel) + Callback Group matching (logically allowed) + node code must thread-safe (if multiple callbacks hit a common variable, mutex self-management —ROS2 does not self-manage this part).
Technical details
Formulas
The logical flow number for MultiThreadedExecutor should refer to the real-world CPU multiplier, not an arbitrary number:
Reasonable num_threads ≈ available CPU cores (or cores minus 1, leaving one for the OS/other processes)
Example: robot computer with 4 CPU cores
num_threads = 4 (use all) or 3 (leave 1 core for the system)
Far exceeding the actual core count (for example, num_threads=16 on a 4-core machine)
does not make it faster — threads compete for CPU time (context switching),
adding overhead rather than actual performance
Parameters
| Parameters | Value |
|---|---|
num_threads default if not specified | Automatically grab detected CPU core count |
| Cost of creating an OS stream | Small but not 0 — creates too many individual executors/routes for each node that can add up significantly on weakly embedded machines |
Practical applications
Multiple add_node() allows multiple nodes to share a common pool of streams, instead of each node individually rclpy.spin() (costing an additional OS stream for each independent spin):
executor = MultiThreadedExecutor()
executor.add_node(motor_node)
executor.add_node(lidar_node)
executor.spin()
This is a useful technique on the limited resource embedded computer of AMR — a script bringup groups multiple lightweight nodes (e. g., diagnostic fusion nodes, general parameter management nodes) into one file 1 executor shares a stream pool, reducing the total number of OS streams to manage compared to each node running by itself rclpy.spin() independent in private process — This is also the technical background underlying Composition learned in the previous article, where many components run the same process often using the same executor of this type.
The table quickly determines when to configure what:
| Situation | Solution |
|---|---|
| Node has only 1-2 simple callbacks, no slow callbacks | SingleThreadedExecutor (default) is sufficient |
| Slow callback (I/O, heavy calculation) to separate from fast callback | MultiThreadedExecutor + ReentrantCallbackGroup cho callback nhanh |
| Multiple nodes need to share computing resources, stream overhead reduced | MultiThreadedExecutor + add_node() multiple times |
| Callback meets variable/state — multiple write callbacks | Manually add threading.Lock() no matter which executor is used |
Practical example
Illustrative scenario (default SingleThreadedExecutor):
[10:00:00.000] Callback save_map starts (file write, takes 3 seconds)
[10:00:00.500] Collision occurs — callback estop_cb should run IMMEDIATELY
[10:00:03.000] Callback save_map finishes
[10:00:03.001] Callback estop_cb FINALLY runs — 2.5 seconds late!
Same scenario, with estop_cb in its own ReentrantCallbackGroup + MultiThreadedExecutor:
[10:00:00.000] Callback save_map starts (thread 1)
[10:00:00.500] Collision occurs
[10:00:00.501] Callback estop_cb runs IMMEDIATELY on thread 2, without waiting for save_map
The 2.5 - second latency difference in the first scenario illustrates exactly why this is a real safety problem, not just a normal "low - speed".
Common mistakes
- Exchange of the executor forgetting to split the Callback Group— as stated in the Operating Principles section, this is the most common error, making the change to
MultiThreadedExecutor"no use" from the outside. - Supplying too many threads compared to the real-world— CPU multiplier as calculated in the Formula section,
num_threadsfar exceeds the physical multiplier only increasing context-switching overhead, without increasing real performance. - Do not automatically secure shared data with locks when using ReentrantCallbackGroup— as mentioned in the Callback Group article, ROS2 does not automatically thread-safeize your code.
- Use MultiThreadedExecutor default "for sure" although unnecessary — add complexity (think callback group, race condition) to simple node without slow callback — which should only be used when there is real need to separate slow/fast callback.
Conclusion
Multi-threading in ROS2 correctly solves the problem of "delay callback blocking fast callback in the same node", not the generic acceleration tool — which needs all 3 elements to go together: enough streams, callback group of the right type, and code thread - safe. The next article in the column, also the penultimate, on Executor — delves deeper into the callback scheduling mechanism that both MultiThreadedExecutor and the Callback Group rely on.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.