CodeTheRobot

Ubuntu Linux for Robotics · Lesson 50 of 96

Multi-threaded executors

Choosing single or multi-threaded execution and checking CPU use in Ubuntu.

Estimated time · 45–75 min

Learning goals

What you will understand

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

Understand the idea first

The mental model

rclcpp::spin runs callbacks one at a time in a single thread. A MultiThreadedExecutor runs callbacks on several threads, so a slow callback no longer blocks the others, provided they are in different callback groups or in a reentrant group. Shared data then needs a mutex.

Use Ubuntu's tools to see the effect: htop -H shows the threads of a process and their CPU use, and ros2 topic hz shows whether a fast timer keeps its rate while a slow callback runs. Do not add threads without measuring first.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the multi-threaded executors commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

htop -p $(pgrep -f timer_demo | head -1)
# Watch one process (press H to show its threads).
ros2 topic hz /fast_timer
# Measure the rate of the fast timer.
top -H -p $(pgrep -f timer_demo | head -1)
# Show per-thread CPU use with top.
ros2 run robot_nodes timer_demo --ros-args -p threads:=1
# Run with one thread.
ros2 run robot_nodes timer_demo --ros-args -p threads:=4
# Run with four threads and compare the rate.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

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 Multi-threaded executors. Expected result: average rate: 100.0

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

# TODO: run the timer demo with four threads

Expected output

average rate: 100.0
Stuck? Show the full solution (try it yourself first)
ros2 run robot_nodes timer_demo --ros-args -p threads:=4

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Run a node with a fast 10 ms timer and a slow 500 ms callback using one thread, record the fast timer's rate, then use a multi-threaded executor with two callback groups and record it again.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.