From 0eb84d2c6d4657c482b3deff8d4114d676197099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Wed, 24 Jan 2024 21:27:32 +0100 Subject: Server: start worker threads after initialization of services --- src/network.cpp | 13 +++++++++---- src/network.h | 2 ++ 2 files changed, 11 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/network.cpp b/src/network.cpp index e63949f..5807381 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -34,9 +34,11 @@ Server::Server(const Head & head, ServerConfig && config): p->services.reserve(config.services.size()); for (const auto & ctor : config.services) p->services.emplace_back(ctor(*this)); + + p->startThreads(); } -Server:: Server(const std::shared_ptr & ptr): +Server::Server(const std::shared_ptr & ptr): p(ptr) { } @@ -366,9 +368,6 @@ Server::Priv::Priv(const Head & local, const Identity & self): laddr.sin6_port = htons(discoveryPort); if (::bind(sock, (sockaddr *) &laddr, sizeof(laddr)) < 0) throw std::system_error(errno, std::generic_category()); - - threadListen = thread([this] { doListen(); }); - threadAnnounce = thread([this] { doAnnounce(); }); } Server::Priv::~Priv() @@ -391,6 +390,12 @@ shared_ptr Server::Priv::getptr() return shared_ptr(this, [](Priv *){}); } +void Server::Priv::startThreads() +{ + threadListen = thread([this] { doListen(); }); + threadAnnounce = thread([this] { doAnnounce(); }); +} + void Server::Priv::doListen() { unique_lock lock(dataMutex); diff --git a/src/network.h b/src/network.h index 12ec4e1..4c23b47 100644 --- a/src/network.h +++ b/src/network.h @@ -89,6 +89,8 @@ struct Server::Priv shared_ptr getptr(); + void startThreads(); + void doListen(); void doAnnounce(); -- cgit v1.2.3