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

ROS2: Preferred method of making worker threads

asked 2018-11-12 06:35:07 -0600

uthinu gravatar image

Hi, what is a preferred method of making worker threads in ROS2? If I would want real-time, should I use independently made rea- time threads, or is there an API for that?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2018-11-12 11:45:23 -0600

William gravatar image

updated 2018-11-12 11:45:42 -0600

There's no API for creating threads in ROS 2. The Executor class calls callbacks (if that's what you mean), and if you want a different threading model from SingleThreadedExecutor and MultiThreadedExecutor then you should make you're own executor, using one of those two as an example. They are fairly simple:

Then you can create an instance of your custom executor, add nodes to it, and then call spin on it.

edit flag offensive delete link more


Any help on the code? It is somewhat cryptic for me, I can not see where the thread is created... I would like a fixed node:thread mapping which does not seem unusual to me, think nodes real-time and nodes non real-time. Any plans of adding such an executor?

uthinu gravatar image uthinu  ( 2018-11-14 09:43:00 -0600 )edit
gvdhoorn gravatar image gvdhoorn  ( 2018-11-14 09:44:25 -0600 )edit

Just add each node to it's own SingleThreadedExecutor, and call spin on each of those executors in their own thread, and you can setup each thread to be a real-time priority thread or not.

William gravatar image William  ( 2018-11-14 11:31:45 -0600 )edit

The only built-in executor that creates a thread is the MultiThreadedExecutor , see:

William gravatar image William  ( 2018-11-14 11:31:54 -0600 )edit

And if I would want this thread:node mapping and also reentrant callbacks? Could I spin each MultiThreaderExecutor in its own thread, and the threads created by that executor in its pool would inherit the parent thread's properties?

uthinu gravatar image uthinu  ( 2018-11-14 11:38:49 -0600 )edit

I don't know, it depends on what priority your operating system gives to new threads. If they are not inherited, then you'd need to make a new MultiThreadedExecutor which modifies the priorities of the threads it creates before using them. But as I said the MultiThreadedExecutor is trivial.

William gravatar image William  ( 2018-11-14 11:47:07 -0600 )edit

I do not know C++ well... Could you point me to an exact line in MultiThreaderExecutor.cpp where the threads are created?

uthinu gravatar image uthinu  ( 2018-11-14 12:09:36 -0600 )edit

I do not know C++ well...

That won't mix well with writing real-time safe code...

I already linked to it, the emplace_back :

William gravatar image William  ( 2018-11-14 12:21:07 -0600 )edit

Question Tools



Asked: 2018-11-12 06:35:07 -0600

Seen: 3,832 times

Last updated: Nov 12 '18