CodeTheRobot

ROS 2 Lyrical Luth · Lesson 15 of 86

Executors: single-threaded vs multi-threaded

How callbacks are scheduled, callback groups and running several nodes in one process.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain executors: single-threaded vs multi-threaded in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

An executor takes ready callbacks (messages, timers, service requests) and runs them. rclcpp::spin uses a SingleThreadedExecutor: callbacks run one at a time, so one slow callback delays all others. A MultiThreadedExecutor runs callbacks on a pool of threads.

Callbacks in the same MutuallyExclusive callback group never overlap; a Reentrant group or different groups can. You can add several nodes to one executor to run them in a single process. Protect shared data with a mutex when using several threads.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the executors: single-threaded vs multi-threaded example and identify what each line does.

  3. 3.

    Build only the affected package, source the workspace overlay, and run the example.

  4. 4.

    Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.

Worked example

Read it before you run it

rclcpp::executors::SingleThreadedExecutor single;
// One thread runs all callbacks in order.
rclcpp::executors::MultiThreadedExecutor multi{rclcpp::ExecutorOptions(), 4};
// A pool of four threads runs callbacks in parallel.
multi.add_node(std::make_shared<SensorNode>());
// Add the first node to the executor.
multi.add_node(std::make_shared<ControlNode>());
// Add a second node: both share one process.
multi.spin();
// Run until shutdown.
auto group = create_callback_group(rclcpp::CallbackGroupType::Reentrant);
// Callbacks in this group may run at the same time.

What the example is doing

  1. 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
  2. 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
  3. 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.

Watch it run · Messages between nodes

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

/topicpublishersubscribersubscribermessages delivered: 0MESSAGES BETWEEN NODES

Demo for Executors: single-threaded vs multi-threaded. Expected result: The 10 ms timer keeps its rate while the slow callback sleeps

Practice code

Now type it yourself

Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.

int main(int argc, char** argv) {
  rclcpp::init(argc, argv);
  auto node = std::make_shared<TimerDemo>();
  // TODO: use a MultiThreadedExecutor with 2 threads, add the node and spin
  rclcpp::shutdown();
  return 0;
}

Expected output

The 10 ms timer keeps its rate while the slow callback sleeps
Stuck? Show the full solution (try it yourself first)
int main(int argc, char** argv) {
  rclcpp::init(argc, argv);
  auto node = std::make_shared<TimerDemo>();
  rclcpp::executors::MultiThreadedExecutor exec{rclcpp::ExecutorOptions(), 2};
  exec.add_node(node);
  exec.spin();
  rclcpp::shutdown();
  return 0;
}

Expected result

How you know it worked

  • ROS_DISTRO is lyrical in every terminal used for the exercise.
  • The complete package from the exercise builds and the expected graph entity appears in a second terminal.

Practical exercise

Apply the lesson

Create a node with a timer callback that sleeps 500 ms and another 10 ms timer. Measure the 10 ms timer's period with a single-threaded executor, then with two callback groups on a multi-threaded executor.

Before you continue

  • ROS_DISTRO reports lyrical in every terminal you used
  • The example builds and runs without errors
  • You checked the result with a ros2 command, RViz2 or a test, not just by reading the code

If something goes wrong

ros2 cannot find a package or executable.

Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.

Nodes run but cannot discover each other.

Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.