From 6d1a124589ccc34d0ce845b3695983d4abb89f17 Mon Sep 17 00:00:00 2001 From: sehe Date: Wed, 20 Jan 2021 02:55:27 +0100 Subject: [PATCH] Fix issue #1 Adds a work_ object to avoid prematurely exiting io_service --- src/msghub_impl.cpp | 7 ++++++- src/msghub_impl.h | 6 ++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/msghub_impl.cpp b/src/msghub_impl.cpp index 9f1546e..cf6c321 100644 --- a/src/msghub_impl.cpp +++ b/src/msghub_impl.cpp @@ -46,12 +46,14 @@ void msghub_impl::initpool(uint8_t threads) msghub_impl::msghub_impl(boost::asio::io_service& io_service) : publisher_(new hubconnection(io_service, *this)) , io_service_(io_service) + , work_(boost::asio::io_service::work(io_service)) , acceptor_(io_service) , initok_(false) {} msghub_impl::~msghub_impl() { + work_.reset(); io_service_.stop(); join(); } @@ -90,9 +92,12 @@ bool msghub_impl::create(uint32_t port, uint8_t threads) void msghub_impl::join() { + work_.reset(); + // Join to all service threads for (auto it : threads_) - it->join(); + if (it->joinable()) + it->join(); } bool msghub_impl::publish(const std::string& topic, const std::vector& message) diff --git a/src/msghub_impl.h b/src/msghub_impl.h index d630ad4..7ae8114 100644 --- a/src/msghub_impl.h +++ b/src/msghub_impl.h @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -30,18 +31,15 @@ using boost::asio::ip::tcp; class msghub_impl : public hub { -public: - private: typedef std::map < std::string, msghub::onmessage >::iterator messagemapit; std::map < std::string, msghub::onmessage > messagemap_; -public: - private: std::vector > threads_; boost::shared_ptr publisher_; boost::asio::io_service& io_service_; + boost::optional work_; tcp::acceptor acceptor_; bool initok_;