Skip to content

Commit 34b7b27

Browse files
committed
Get rid of the std::thread::detach().
1 parent db788bd commit 34b7b27

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

SerialPrograms/Source/Integrations/DppIntegration/DppClient.cpp

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,28 @@
99
#include "DppClient.h"
1010
#include "DppCommandHandler.h"
1111

12-
using namespace dpp;
12+
//#include <iostream>
13+
//using std::cout;
14+
//using std::endl;
15+
1316
namespace PokemonAutomation{
1417
namespace Integration{
1518
namespace DppClient{
1619

20+
using namespace dpp;
21+
1722

1823
Client& Client::instance(){
1924
static Client client;
2025
return client;
2126
}
2227

28+
29+
30+
Client::~Client(){
31+
disconnect();
32+
}
33+
2334
bool Client::is_initialized(){
2435
std::lock_guard<std::mutex> lg(m_client_lock);
2536
return m_bot != nullptr;
@@ -41,7 +52,7 @@ void Client::connect(){
4152
m_bot = std::make_unique<cluster>(token, intents);
4253
m_handler = std::make_unique<commandhandler>(m_bot.get(), false);
4354
m_bot->cache_policy = { cache_policy_setting_t::cp_lazy, cache_policy_setting_t::cp_lazy, cache_policy_setting_t::cp_aggressive };
44-
std::thread(&Client::run, this, token).detach();
55+
m_start_thread = std::thread(&Client::run, this, token);
4556
}catch (std::exception& e){
4657
Handler::log_dpp("DPP thew an exception: " + (std::string)e.what(), "connect()", ll_critical);
4758
}
@@ -50,15 +61,22 @@ void Client::connect(){
5061

5162
void Client::disconnect(){
5263
std::lock_guard<std::mutex> lg(m_client_lock);
53-
if (m_bot != nullptr && m_is_connected.load(std::memory_order_relaxed)){
54-
try{
55-
m_bot->shutdown();
56-
m_handler.reset();
57-
m_bot.reset();
58-
m_is_connected.store(false, std::memory_order_release);
59-
}catch (std::exception& e){
60-
Handler::log_dpp("DPP thew an exception: " + (std::string)e.what(), "disconnect()", ll_critical);
61-
}
64+
// cout << "Client::disconnect()" << endl;
65+
66+
if (m_start_thread.joinable()){
67+
m_start_thread.join();
68+
}
69+
70+
if (m_bot == nullptr || !m_is_connected.load(std::memory_order_relaxed)){
71+
return;
72+
}
73+
try{
74+
m_bot->shutdown();
75+
m_handler.reset();
76+
m_bot.reset();
77+
m_is_connected.store(false, std::memory_order_release);
78+
}catch (std::exception& e){
79+
Handler::log_dpp("DPP thew an exception: " + (std::string)e.what(), "disconnect()", ll_critical);
6280
}
6381
}
6482

@@ -149,6 +167,8 @@ void Client::run(const std::string& token){
149167
m_bot.reset();
150168
m_is_connected.store(false, std::memory_order_release);
151169
}
170+
171+
// cout << "Client::run() - ending" << endl;
152172
}
153173

154174

SerialPrograms/Source/Integrations/DppIntegration/DppClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace DppClient{
1717
class Client : protected DppCommandHandler::Handler{
1818
public:
1919
Client() : m_is_connected(false) {}
20+
~Client();
2021
static Client& instance();
2122

2223
public:
@@ -41,6 +42,7 @@ class Client : protected DppCommandHandler::Handler{
4142
std::unique_ptr<dpp::commandhandler> m_handler = nullptr;
4243
std::atomic<bool> m_is_connected;
4344
std::mutex m_client_lock;
45+
std::thread m_start_thread;
4446
};
4547

4648

0 commit comments

Comments
 (0)