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()
{
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()) {

@ -22,7 +22,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include <string>
#include <future>
#include <thread>
#include <atomic>
#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<bool> m_terminateThread{false};
IRCReceiver *m_recv;
IRCMessageQueue *m_recvQ;
IRCMessageQueue *m_sendQ;
IRCProtocol *m_proto;
IRCApplication *m_app;
std::future<void> m_future;
};
std::thread m_thread;
};

@ -112,7 +112,7 @@ public:
std::regex m_fromPattern;
bool m_initReady;
bool m_terminateThread;
std::atomic<bool> m_terminateThread{false};
std::map<std::string, IRCDDBAppUserObject> m_userMap;
std::mutex m_userMapMutex;
@ -151,6 +151,8 @@ IRCDDBApp::IRCDDBApp(const std::string& u_chan)
IRCDDBApp::~IRCDDBApp()
{
stopWork();
delete m_d->m_sendQ;
delete m_d;
}
@ -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) {

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

Loading…
Cancel
Save

Powered by TurnKey Linux.