Skip to content

Commit

Permalink
Fix issue #1
Browse files Browse the repository at this point in the history
Adds a work_ object to avoid prematurely exiting io_service
  • Loading branch information
sehe committed Jan 20, 2021
1 parent 021f381 commit 6d1a124
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/msghub_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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<char>& message)
Expand Down
6 changes: 2 additions & 4 deletions src/msghub_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <set>

#include <boost/bind.hpp>
#include <boost/optional.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
Expand All @@ -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<boost::shared_ptr<boost::thread> > threads_;
boost::shared_ptr<hubconnection> publisher_;
boost::asio::io_service& io_service_;
boost::optional<boost::asio::io_service::work> work_;
tcp::acceptor acceptor_;
bool initok_;

Expand Down

0 comments on commit 6d1a124

Please sign in to comment.