From 708aab165af4a04199cd5b47b5afb41159e208c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Thu, 7 Mar 2024 15:08:14 -0500 Subject: [PATCH] [timer] Threading fixes by using asio::dispatch instead of post --- src/ossia/detail/timer.hpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/ossia/detail/timer.hpp b/src/ossia/detail/timer.hpp index 32628cafed3..8aae5a1afd8 100644 --- a/src/ossia/detail/timer.hpp +++ b/src/ossia/detail/timer.hpp @@ -31,23 +31,28 @@ class timer { m_timer.expires_from_now(m_delay); m_timer.async_wait([this, ff = std::move(f)](auto ec) { - if(ec) + if(ec == boost::asio::error::operation_aborted) + return; + else if(ec) { ossia::logger().error("timer error: {}", ec.message()); return; } - - ff(); - this->start(std::move(ff)); + else + { + ff(); + this->start(std::move(ff)); + } }); } void stop() { + boost::asio::dispatch( + m_timer.get_executor(), [tm = std::make_shared( + std::move(m_timer))]() mutable { tm->cancel(); }); std::future wait - = boost::asio::post(m_timer.get_executor(), boost::asio::use_future); - m_ctx->post([tm = std::make_shared( - std::move(m_timer))]() mutable { tm->cancel(); }); + = boost::asio::dispatch(m_timer.get_executor(), boost::asio::use_future); wait.get(); }