Skip to content

Commit

Permalink
Set the minimum number of threads of the Multithreaded executor to 2 (r…
Browse files Browse the repository at this point in the history
…os2#2030)

* Set the minimum number of threads of the Multithreaded executor to 2

Signed-off-by: Alexis Paques <[email protected]>
  • Loading branch information
AlexisTM committed Oct 25, 2022
1 parent 2d32d03 commit 85c0af4
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions rclcpp/src/rclcpp/executors/multi_threaded_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "rcpputils/scope_exit.hpp"

#include "rclcpp/logging.hpp"
#include "rclcpp/utilities.hpp"

using rclcpp::executors::MultiThreadedExecutor;
Expand All @@ -34,9 +35,15 @@ MultiThreadedExecutor::MultiThreadedExecutor(
yield_before_execute_(yield_before_execute),
next_exec_timeout_(next_exec_timeout)
{
number_of_threads_ = number_of_threads ? number_of_threads : std::thread::hardware_concurrency();
if (number_of_threads_ == 0) {
number_of_threads_ = 1;
number_of_threads_ = number_of_threads > 0 ?
number_of_threads :
std::max(std::thread::hardware_concurrency(), 2U);

if (number_of_threads_ == 1) {
RCLCPP_WARN(
rclcpp::get_logger("rclcpp"),
"MultiThreadedExecutor is used with a single thread.\n"
"Use the SingleThreadedExecutor instead.");
}
}

Expand Down

0 comments on commit 85c0af4

Please sign in to comment.