From dd1f38b4c86c4890e8b6a662f2fbac34d1b88a1c Mon Sep 17 00:00:00 2001 From: Jackie Kay Date: Fri, 4 Dec 2015 13:31:12 -0800 Subject: [PATCH] Get rid of idle thread in MultiThreadedExecutor --- rclcpp/src/rclcpp/executors/multi_threaded_executor.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/rclcpp/src/rclcpp/executors/multi_threaded_executor.cpp b/rclcpp/src/rclcpp/executors/multi_threaded_executor.cpp index fc393f4..d419ffd 100644 --- a/rclcpp/src/rclcpp/executors/multi_threaded_executor.cpp +++ b/rclcpp/src/rclcpp/executors/multi_threaded_executor.cpp @@ -42,15 +42,17 @@ MultiThreadedExecutor::spin() } RCLCPP_SCOPE_EXIT(this->spinning.store(false); ); std::vector threads; + size_t thread_id = 0; { std::lock_guard wait_lock(wait_mutex_); - size_t thread_id = 1; - for (size_t i = number_of_threads_; i > 0; --i) { + for (; thread_id < number_of_threads_ - 1; ++thread_id) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); - auto func = std::bind(&MultiThreadedExecutor::run, this, thread_id++); + auto func = std::bind(&MultiThreadedExecutor::run, this, thread_id); threads.emplace_back(func); } } + + run(thread_id); for (auto & thread : threads) { thread.join(); }