# micro-ROS Multithreading support

**URL:** <https://discourse.openrobotics.org/t/micro-ros-multithreading-support/21257>\
**Category:** Embedded\
**Tags:** ros2, embedded, foxy, galactic, micro-ros\
**Created:** [July 7, 2021, 12:56pm UTC](https://discourse.openrobotics.org/t/micro-ros-multithreading-support/21257 "2021-07-07T12:56:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mamerlan](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/mamerlan/32/11472_2.png) [@mamerlan](https://discourse.openrobotics.org/u/mamerlan)\
**Post date:** [July 7, 2021, 12:56pm UTC](https://discourse.openrobotics.org/t/micro-ros-multithreading-support/21257/1 "2021-07-07T12:56:14Z")

</div>

**[micro-ROS](https://micro.ros.org/) Foxy, Galactic and Rolling provides a new enhancement, the multithreading feature.**

Multithreading allows the micro-ROS default middleware (Micro XRCE-DDS) to be thread safe. This way, micro-ROS nodes can run ROS 2 entities such as publishers, subscribers or services in different RTOS execution threads.

As a result, micro-ROS will run in a multi threaded approach under specific circumstances, improving the performance, the usability and resource optimization.

The multithreading mechanism together with [shared memory transport](http://discourse.ros.org/t/shared-memory-implementation-available-now-in-micro-ros/20422?u=mamerlan) contribute to provide a highly flexible environment for deploying micro-ROS nodes with isolated functionality in different RTOS tasks or threads. As always, in a build-time configured way with no usage of dynamic memory.

To activate this new feature, just enable the Multithreading[flag](https://github.com/eProsima/Micro-XRCE-DDS-Client/blob/3e2a144b44add2a789ecdfd116686c981738839e/CMakeLists.txt#L65) in your middleware configuration using your colcon.meta file.

These features are supported in Micro XRCE-DDS, the [micro-ROS](https://micro.ros.org/) middleware implementation.  
See the associated PRs in the Micro XRCE-DDS library ([General](https://github.com/eProsima/Micro-XRCE-DDS/pull/85), [Client](https://github.com/eProsima/Micro-XRCE-DDS-Client/pull/224), [Agent](https://github.com/eProsima/Micro-XRCE-DDS-Agent/pull/239)) and in micro-ROS’ [Client RMW](https://github.com/micro-ROS/rmw-microxrcedds/pull/123) and [Agent](https://github.com/eProsima/Micro-XRCE-DDS-Agent/pull/239).

---

<div class="post-metadata">

**Author:** ![BrettRD](https://avatars.discourse-cdn.com/v4/letter/b/e47c2d/32.png) [@BrettRD](https://discourse.openrobotics.org/u/BrettRD)\
**Post date:** [July 8, 2021, 2:28am UTC](https://discourse.openrobotics.org/t/micro-ros-multithreading-support/21257/2 "2021-07-08T02:28:59Z")

</div>

This is super exciting!  
Are any improvements needed in other packages like rclc to make micro-ros as a whole thread-safe?

---

<div class="post-metadata">

**Author:** ![pablogs](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/pablogs/32/7568_2.png) [@pablogs](https://discourse.openrobotics.org/u/pablogs)\
**Post date:** [July 8, 2021, 5:28am UTC](https://discourse.openrobotics.org/t/micro-ros-multithreading-support/21257/3 "2021-07-08T05:28:25Z")

</div>

Thanks, @BrettRD. One of our main concerns right now is that all `*_init_*()` functions are not thread-safe. Check [here](https://github.com/ros2/rclc/blob/ae58a72168f791df6a328ed4324d8a3b156e1e1b/rclc/include/rclc/publisher.h#L38) for example.
