From 44ca95f961cd9bc1c5ddf13cb7bc2451bcebe448 Mon Sep 17 00:00:00 2001 From: Marko Durkovic Date: Mon, 28 Jan 2019 16:35:46 +0100 Subject: [PATCH] Avoid race that triggers timer too often The two distinct operations of acquiring and subsequent checking of a timer have to be protected by one lock_guard against races with other threads. The releasing of a timer has to be protected by the same lock. Given this requirement there is no use for a second mutex. --- rclcpp/include/rclcpp/executors/multi_threaded_executor.hpp | 1 - rclcpp/src/rclcpp/executors/multi_threaded_executor.cpp | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/rclcpp/include/rclcpp/executors/multi_threaded_executor.hpp b/rclcpp/include/rclcpp/executors/multi_threaded_executor.hpp index eb41a93e11..72df5dd82a 100644 --- a/rclcpp/include/rclcpp/executors/multi_threaded_executor.hpp +++ b/rclcpp/include/rclcpp/executors/multi_threaded_executor.hpp @@ -78,7 +78,6 @@ class MultiThreadedExecutor : public executor::Executor size_t number_of_threads_; bool yield_before_execute_; - std::mutex scheduled_timers_mutex_; std::set scheduled_timers_; }; diff --git a/rclcpp/src/rclcpp/executors/multi_threaded_executor.cpp b/rclcpp/src/rclcpp/executors/multi_threaded_executor.cpp index 08318534b2..6c04d81315 100644 --- a/rclcpp/src/rclcpp/executors/multi_threaded_executor.cpp +++ b/rclcpp/src/rclcpp/executors/multi_threaded_executor.cpp @@ -82,7 +82,6 @@ MultiThreadedExecutor::run(size_t) } if (any_exec.timer) { // Guard against multiple threads getting the same timer. - std::lock_guard lock(scheduled_timers_mutex_); if (scheduled_timers_.count(any_exec.timer) != 0) { continue; } @@ -96,7 +95,7 @@ MultiThreadedExecutor::run(size_t) execute_any_executable(any_exec); if (any_exec.timer) { - std::lock_guard lock(scheduled_timers_mutex_); + std::lock_guard wait_lock(wait_mutex_); auto it = scheduled_timers_.find(any_exec.timer); if (it != scheduled_timers_.end()) { scheduled_timers_.erase(it);