From cf8e19684da76cbf26c33aac9b1294873e0e479e Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sat, 23 Aug 2025 14:55:06 +0200 Subject: [PATCH] change from future to actual thread #58 --- IRCDDB/IRCClient.cpp | 21 +++++++++++++-------- IRCDDB/IRCClient.h | 9 +++++---- IRCDDB/IRCDDBApp.cpp | 22 ++++++++++++++-------- IRCDDB/IRCDDBApp.h | 6 ++++-- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/IRCDDB/IRCClient.cpp b/IRCDDB/IRCClient.cpp index dcdc3cf..d46b32b 100644 --- a/IRCDDB/IRCClient.cpp +++ b/IRCDDB/IRCClient.cpp @@ -58,19 +58,24 @@ IRCClient::IRCClient(IRCApplication *app, const std::string& update_channel, con IRCClient::~IRCClient() { - delete m_proto; + stopWork(); + delete m_proto; } void IRCClient::startWork() { - m_terminateThread = false; - m_future = std::async(std::launch::async, &IRCClient::Entry, this); + if (m_thread.joinable()) + return; + + m_terminateThread.store(false, std::memory_order_relaxed); + m_thread = std::thread(&IRCClient::Entry, this); } void IRCClient::stopWork() { - m_terminateThread = true; - m_future.get(); + m_terminateThread.store(true, std::memory_order_relaxed); + if (m_thread.joinable()) + m_thread.join(); } void IRCClient::Entry() @@ -98,7 +103,7 @@ void IRCClient::Entry() switch (state) { case 0: - if (m_terminateThread) { + if (m_terminateThread.load(std::memory_order_relaxed)) { CLog::logInfo("IRCClient::Entry: thread terminated at state=%d\n", state); return; } @@ -118,7 +123,7 @@ void IRCClient::Entry() break; case 1: - if (m_terminateThread) { + if (m_terminateThread.load(std::memory_order_relaxed)) { CLog::logInfo("IRCClient::Entry: thread terminated at state=%d\n", state); return; } @@ -261,7 +266,7 @@ void IRCClient::Entry() case 5: - if (m_terminateThread) + if (m_terminateThread.load(std::memory_order_relaxed)) state = 6; else { if (m_recvQ->isEOF()) { diff --git a/IRCDDB/IRCClient.h b/IRCDDB/IRCClient.h index 72b6c97..2a1a023 100644 --- a/IRCDDB/IRCClient.h +++ b/IRCDDB/IRCClient.h @@ -22,7 +22,8 @@ along with this program. If not, see . #pragma once #include -#include +#include +#include #include "IRCReceiver.h" #include "IRCMessageQueue.h" @@ -47,13 +48,13 @@ private: unsigned int m_port; std::string m_callsign; std::string m_password; - bool m_terminateThread; + std::atomic m_terminateThread{false}; IRCReceiver *m_recv; IRCMessageQueue *m_recvQ; IRCMessageQueue *m_sendQ; IRCProtocol *m_proto; IRCApplication *m_app; - std::future m_future; -}; + std::thread m_thread; +}; \ No newline at end of file diff --git a/IRCDDB/IRCDDBApp.cpp b/IRCDDB/IRCDDBApp.cpp index 58909de..862ed24 100644 --- a/IRCDDB/IRCDDBApp.cpp +++ b/IRCDDB/IRCDDBApp.cpp @@ -112,7 +112,7 @@ public: std::regex m_fromPattern; bool m_initReady; - bool m_terminateThread; + std::atomic m_terminateThread{false}; std::map m_userMap; std::mutex m_userMapMutex; @@ -151,8 +151,10 @@ IRCDDBApp::IRCDDBApp(const std::string& u_chan) IRCDDBApp::~IRCDDBApp() { - delete m_d->m_sendQ; - delete m_d; + stopWork(); + + delete m_d->m_sendQ; + delete m_d; } void IRCDDBApp::rptrQTH(const std::string& callsign, double latitude, double longitude, const std::string& desc1, const std::string& desc2, const std::string& infoURL) @@ -279,14 +281,18 @@ IRCMessage *IRCDDBApp::getReplyMessage() void IRCDDBApp::startWork() { - m_d->m_terminateThread = false; - m_future = std::async(std::launch::async, &IRCDDBApp::Entry, this); + if (m_thread.joinable()) + return; + + m_d->m_terminateThread.store(false, std::memory_order_relaxed); + m_thread = std::thread(&IRCDDBApp::Entry, this); } void IRCDDBApp::stopWork() { - m_d->m_terminateThread = true; - m_future.get(); + m_d->m_terminateThread.store(true, std::memory_order_relaxed); + if (m_thread.joinable()) + m_thread.join(); } unsigned int IRCDDBApp::calculateUsn(const std::string& nick) @@ -996,7 +1002,7 @@ static bool needsDatabaseUpdate(int tableID) void IRCDDBApp::Entry() { int sendlistTableID = 0; - while (!m_d->m_terminateThread) { + while (!m_d->m_terminateThread.load(std::memory_order_relaxed)) { if (m_d->m_timer > 0) m_d->m_timer--; switch(m_d->m_state) { diff --git a/IRCDDB/IRCDDBApp.h b/IRCDDB/IRCDDBApp.h index a53ed34..85f6251 100644 --- a/IRCDDB/IRCDDBApp.h +++ b/IRCDDB/IRCDDBApp.h @@ -25,7 +25,8 @@ along with this program. If not, see . #include "IRCApplication.h" #include -#include +#include +#include #include #include @@ -99,6 +100,7 @@ private: IRCDDBAppPrivate *m_d; time_t m_maxTime; - std::future m_future; + std::thread m_thread; + };