Skip to content

Commit

Permalink
[timer] Threading fixes by using asio::dispatch instead of post
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Mar 7, 2024
1 parent d95b3bc commit 708aab1
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/ossia/detail/timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<boost::asio::steady_timer>(
std::move(m_timer))]() mutable { tm->cancel(); });
std::future<void> wait
= boost::asio::post(m_timer.get_executor(), boost::asio::use_future);
m_ctx->post([tm = std::make_shared<boost::asio::steady_timer>(
std::move(m_timer))]() mutable { tm->cancel(); });
= boost::asio::dispatch(m_timer.get_executor(), boost::asio::use_future);
wait.get();
}

Expand Down

0 comments on commit 708aab1

Please sign in to comment.