change from future to actual thread #58

develop^2^2
Geoffrey Merck 4 months ago
parent ee36452b01
commit cf8e19684d

@ -58,19 +58,24 @@ IRCClient::IRCClient(IRCApplication *app, const std::string& update_channel, con
IRCClient::~IRCClient() IRCClient::~IRCClient()
{ {
delete m_proto; stopWork();
delete m_proto;
} }
void IRCClient::startWork() void IRCClient::startWork()
{ {
m_terminateThread = false; if (m_thread.joinable())
m_future = std::async(std::launch::async, &IRCClient::Entry, this); return;
m_terminateThread.store(false, std::memory_order_relaxed);
m_thread = std::thread(&IRCClient::Entry, this);
} }
void IRCClient::stopWork() void IRCClient::stopWork()
{ {
m_terminateThread = true; m_terminateThread.store(true, std::memory_order_relaxed);
m_future.get(); if (m_thread.joinable())
m_thread.join();
} }
void IRCClient::Entry() void IRCClient::Entry()
@ -98,7 +103,7 @@ void IRCClient::Entry()
switch (state) { switch (state) {
case 0: case 0:
if (m_terminateThread) { if (m_terminateThread.load(std::memory_order_relaxed)) {
CLog::logInfo("IRCClient::Entry: thread terminated at state=%d\n", state); CLog::logInfo("IRCClient::Entry: thread terminated at state=%d\n", state);
return; return;
} }
@ -118,7 +123,7 @@ void IRCClient::Entry()
break; break;
case 1: case 1:
if (m_terminateThread) { if (m_terminateThread.load(std::memory_order_relaxed)) {
CLog::logInfo("IRCClient::Entry: thread terminated at state=%d\n", state); CLog::logInfo("IRCClient::Entry: thread terminated at state=%d\n", state);
return; return;
} }
@ -261,7 +266,7 @@ void IRCClient::Entry()
case 5: case 5:
if (m_terminateThread) if (m_terminateThread.load(std::memory_order_relaxed))
state = 6; state = 6;
else { else {
if (m_recvQ->isEOF()) { if (m_recvQ->isEOF()) {

@ -22,7 +22,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#pragma once #pragma once
#include <string> #include <string>
#include <future> #include <thread>
#include <atomic>
#include "IRCReceiver.h" #include "IRCReceiver.h"
#include "IRCMessageQueue.h" #include "IRCMessageQueue.h"
@ -47,13 +48,13 @@ private:
unsigned int m_port; unsigned int m_port;
std::string m_callsign; std::string m_callsign;
std::string m_password; std::string m_password;
bool m_terminateThread; std::atomic<bool> m_terminateThread{false};
IRCReceiver *m_recv; IRCReceiver *m_recv;
IRCMessageQueue *m_recvQ; IRCMessageQueue *m_recvQ;
IRCMessageQueue *m_sendQ; IRCMessageQueue *m_sendQ;
IRCProtocol *m_proto; IRCProtocol *m_proto;
IRCApplication *m_app; IRCApplication *m_app;
std::future<void> m_future; std::thread m_thread;
};
};

@ -112,7 +112,7 @@ public:
std::regex m_fromPattern; std::regex m_fromPattern;
bool m_initReady; bool m_initReady;
bool m_terminateThread; std::atomic<bool> m_terminateThread{false};
std::map<std::string, IRCDDBAppUserObject> m_userMap; std::map<std::string, IRCDDBAppUserObject> m_userMap;
std::mutex m_userMapMutex; std::mutex m_userMapMutex;
@ -151,8 +151,10 @@ IRCDDBApp::IRCDDBApp(const std::string& u_chan)
IRCDDBApp::~IRCDDBApp() IRCDDBApp::~IRCDDBApp()
{ {
delete m_d->m_sendQ; stopWork();
delete m_d;
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) 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() void IRCDDBApp::startWork()
{ {
m_d->m_terminateThread = false; if (m_thread.joinable())
m_future = std::async(std::launch::async, &IRCDDBApp::Entry, this); return;
m_d->m_terminateThread.store(false, std::memory_order_relaxed);
m_thread = std::thread(&IRCDDBApp::Entry, this);
} }
void IRCDDBApp::stopWork() void IRCDDBApp::stopWork()
{ {
m_d->m_terminateThread = true; m_d->m_terminateThread.store(true, std::memory_order_relaxed);
m_future.get(); if (m_thread.joinable())
m_thread.join();
} }
unsigned int IRCDDBApp::calculateUsn(const std::string& nick) unsigned int IRCDDBApp::calculateUsn(const std::string& nick)
@ -996,7 +1002,7 @@ static bool needsDatabaseUpdate(int tableID)
void IRCDDBApp::Entry() void IRCDDBApp::Entry()
{ {
int sendlistTableID = 0; int sendlistTableID = 0;
while (!m_d->m_terminateThread) { while (!m_d->m_terminateThread.load(std::memory_order_relaxed)) {
if (m_d->m_timer > 0) if (m_d->m_timer > 0)
m_d->m_timer--; m_d->m_timer--;
switch(m_d->m_state) { switch(m_d->m_state) {

@ -25,7 +25,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "IRCApplication.h" #include "IRCApplication.h"
#include <string> #include <string>
#include <future> #include <thread>
#include <atomic>
#include <ctime> #include <ctime>
#include <vector> #include <vector>
@ -99,6 +100,7 @@ private:
IRCDDBAppPrivate *m_d; IRCDDBAppPrivate *m_d;
time_t m_maxTime; time_t m_maxTime;
std::future<void> m_future; std::thread m_thread;
}; };

Loading…
Cancel
Save

Powered by TurnKey Linux.