From d5b60ac6ac41987216c5ed47f1963181247b2cb6 Mon Sep 17 00:00:00 2001 From: Bo Chen Date: Tue, 8 Feb 2022 14:59:46 +0100 Subject: [PATCH] Fix race condition in shutdown behavior --- .../include/socketcan_interface/asio_base.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/socketcan_interface/include/socketcan_interface/asio_base.h b/socketcan_interface/include/socketcan_interface/asio_base.h index cf36ca145..554322e45 100644 --- a/socketcan_interface/include/socketcan_interface/asio_base.h +++ b/socketcan_interface/include/socketcan_interface/asio_base.h @@ -30,6 +30,7 @@ template class AsioDriver : public DriverInterface{ } protected: + boost::thread post_thread_; boost::asio::io_service io_service_; #if BOOST_ASIO_VERSION >= 101200 // Boost 1.66+ boost::asio::io_context::strand strand_; @@ -86,7 +87,12 @@ template class AsioDriver : public DriverInterface{ {} public: - virtual ~AsioDriver() { shutdown_internal(); } + virtual ~AsioDriver() { + if (post_thread_.joinable()){ + post_thread_.join(); + } + shutdown_internal(); + } State getState(){ boost::mutex::scoped_lock lock(state_mutex_); @@ -100,7 +106,9 @@ template class AsioDriver : public DriverInterface{ boost::asio::io_service::work work(io_service_); setDriverState(State::ready); - boost::thread post_thread([this]() { io_service_.run(); }); + if (!post_thread_.joinable()) { + post_thread_ = boost::thread([this]() { io_service_.run(); }); + } triggerReadSome();