From a8a3287ebf340761d087bdb9c75139c66e0c3461 Mon Sep 17 00:00:00 2001 From: Maschell Date: Thu, 6 Oct 2022 23:07:36 +0200 Subject: [PATCH] Fix joining the thread --- src/main.cpp | 16 ++++++---------- src/utils/CThread.h | 19 +++++-------------- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 78805ff..9006038 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,23 +28,19 @@ INITIALIZE_PLUGIN() { /* Entry point */ ON_APPLICATION_START() { initLogging(); - - if (thread != nullptr) { - DEBUG_FUNCTION_LINE_WARN("The wiiload thread is still allocated but not running."); - thread->skipJoin = true; - delete thread; - thread = nullptr; - } DEBUG_FUNCTION_LINE("Start wiiload thread"); - thread = new TcpReceiver(4299); + thread = new (std::nothrow) TcpReceiver(4299); + if (thread == nullptr) { + DEBUG_FUNCTION_LINE_ERR("Failed to create wiiload thread"); + } } ON_APPLICATION_ENDS() { - DEBUG_FUNCTION_LINE("Stop wiiload thread"); + DEBUG_FUNCTION_LINE("Stop wiiload thread!"); if (thread != nullptr) { delete thread; thread = nullptr; } - + DEBUG_FUNCTION_LINE_VERBOSE("Done!"); deinitLogging(); } \ No newline at end of file diff --git a/src/utils/CThread.h b/src/utils/CThread.h index cb5f3ae..3bce2ed 100644 --- a/src/utils/CThread.h +++ b/src/utils/CThread.h @@ -100,20 +100,12 @@ public: //! Shutdown thread void shutdownThread() { - if (skipJoin) { - DEBUG_FUNCTION_LINE_WARN("Skip joining the thread \"%s\", it's not running.", pThreadName.c_str()); - } else { - //! wait for thread to finish - if (!skipJoin && pThread && !(iAttributes & eAttributeDetach)) { - if (!OSIsThreadTerminated(pThread)) { - if (isThreadSuspended()) { - resumeThread(); - } - OSJoinThread(pThread, nullptr); - } else { - DEBUG_FUNCTION_LINE_WARN("Thread \"%s\" has already been terminated!!!", pThreadName.c_str()); - } + //! wait for thread to finish + if (pThread && !(iAttributes & eAttributeDetach)) { + if (isThreadSuspended()) { + resumeThread(); } + OSJoinThread(pThread, nullptr); } //! free the thread stack buffer @@ -137,7 +129,6 @@ public: eAttributeDetach = 0x08, eAttributePinnedAff = 0x10 }; - bool skipJoin = false; private: static int threadCallback(int argc, const char **argv) {