From 6f0e49d3f8f089ca27b97fd3c93df0702ed34860 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Tue, 2 Sep 2014 10:45:08 -0700 Subject: [PATCH] MTE: store number of threads and provide accessor --- .../executors/multi_threaded_executor.hpp | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/rclcpp/include/rclcpp/executors/multi_threaded_executor.hpp b/rclcpp/include/rclcpp/executors/multi_threaded_executor.hpp index 9648de2..908c68d 100644 --- a/rclcpp/include/rclcpp/executors/multi_threaded_executor.hpp +++ b/rclcpp/include/rclcpp/executors/multi_threaded_executor.hpp @@ -42,22 +42,25 @@ class MultiThreadedExecutor : public executor::Executor public: RCLCPP_MAKE_SHARED_DEFINITIONS(MultiThreadedExecutor); - MultiThreadedExecutor() {} + MultiThreadedExecutor() + { + number_of_threads_ = std::thread::hardware_concurrency(); + if (number_of_threads_ == 0) + { + number_of_threads_ = 1; + } + } ~MultiThreadedExecutor() {} - void spin() + void + spin() { std::vector threads; - size_t number_of_threads = std::thread::hardware_concurrency(); - if (number_of_threads == 0) - { - number_of_threads = 1; - } { std::lock_guard wait_lock(wait_mutex_); size_t thread_id = 1; - for (; number_of_threads > 0; --number_of_threads) + for (size_t i = number_of_threads_; i > 0; --i) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); auto func = std::bind(&MultiThreadedExecutor::run, this, thread_id++); @@ -70,6 +73,12 @@ public: } } + size_t + get_number_of_threads() + { + return number_of_threads_; + } + private: void run(size_t this_thread_id) { @@ -92,6 +101,7 @@ private: RCLCPP_DISABLE_COPY(MultiThreadedExecutor); std::mutex wait_mutex_; + size_t number_of_threads_; };