Skip to content

Commit d97ff9a

Browse files
authored
Merge pull request #1 from jefferyyjhsu/Adding-TimersManager-stop-function-mutex
Add mutex to prevent race condition in TimersManager destructor
2 parents c49b672 + 89c16c5 commit d97ff9a

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

irobot_events_executor/include/rclcpp/timers_manager.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@ class TimersManager
486486
std::thread timers_thread_;
487487
// Protects access to timers
488488
std::mutex timers_mutex_;
489+
// Protects access to stop()
490+
std::mutex stop_mutex_;
489491
// Notifies the timers thread whenever timers are added/removed
490492
std::condition_variable timers_cv_;
491493
// Flag used as predicate by timers_cv_ that denotes one or more timers being added/removed

irobot_events_executor/src/rclcpp/timers_manager.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ void TimersManager::start()
5959

6060
void TimersManager::stop()
6161
{
62+
// Lock stop() function to prevent race condition in destructor
63+
std::unique_lock<std::mutex> lock(stop_mutex_);
6264
// Nothing to do if the timers thread is not running
6365
// or if another thread already signaled to stop.
6466
if (!running_.exchange(false)) {

0 commit comments

Comments
 (0)