Selaa lähdekoodia

修改线程模块解决偶发的问题

lwk 1 viikko sitten
vanhempi
commit
9266abac4c
1 muutettua tiedostoa jossa 142 lisäystä ja 44 poistoa
  1. 142 44
      CcosThread/CcosThread.cpp

+ 142 - 44
CcosThread/CcosThread.cpp

@@ -37,7 +37,19 @@ Thread_Base::Thread_Base(void)
 
 Thread_Base::~Thread_Base(void)
 {
-    StopThread();
+    try{
+        // 温和停止线程,避免在析构中抛出异常
+        if (m_ThreadRunning) {
+            StopThread(); 
+        }
+    }
+        catch (const std::exception& e) {
+        ERR_LOG("[Thread] Exception in ~Thread_Base: " << e.what());
+    }
+    catch (...) {
+        ERR_LOG("[Thread] Unknown exception in ~Thread_Base");
+    }
+
     pthread_cond_destroy(&m_StateCond);
 }
 
@@ -50,12 +62,12 @@ PVOID Thread_Base::GetLogger() {
 }
 
 void* Thread_Base::Thread_Base_Thread(void* pPara) {
+    Thread_Base* handle = static_cast<Thread_Base*>(pPara);
+    if (!handle) {
+        ERR_LOG("Invalid thread parameter");
+        return nullptr;
+    }
     try {
-        Thread_Base* handle = static_cast<Thread_Base*>(pPara);
-        if (!handle) {
-            ERR_LOG("Invalid thread parameter");
-            return nullptr;
-        }
         usleep(30000); // Sleep(30) 替代
 
         if (!handle->RegistThread()) {
@@ -82,18 +94,43 @@ void* Thread_Base::Thread_Base_Thread(void* pPara) {
 
     endwork_entry:
         LOG("Thread " << handle->m_strName << " exiting");
-        handle->ThreadExitProcedure();
+        try {
+            handle->ThreadExitProcedure();
+        }
+        catch (const std::exception& e) {
+            ERR_LOG("[Thread] ThreadExitProcedure exception in " << handle->m_strName << ": " << e.what());
+        }
+        catch (...) {
+            ERR_LOG("[Thread] Unknown ThreadExitProcedure exception in " << handle->m_strName);
+        }
         return nullptr;
     }
     catch (const std::exception& e) {
-        ERR_LOG("[Thread] Exception: " << e.what());
+        ERR_LOG("[Thread] Exception in thread " << handle->m_strName << ": " << e.what());
+
+        // 异常情况下也要执行清理
+        try {
+            handle->ThreadExitProcedure();
+        }
+        catch (...) {
+            ERR_LOG("[Thread] Cleanup failed after exception in " << handle->m_strName);
+        }
+
         return nullptr;
     }
     catch (...) {
-        ERR_LOG("[Thread] Unknown exception");
+        ERR_LOG("[Thread] Unknown exception in thread " << handle->m_strName);
+
+        // 异常情况下也要执行清理
+        try {
+            handle->ThreadExitProcedure();
+        }
+        catch (...) {
+            ERR_LOG("[Thread] Cleanup failed after unknown exception in " << handle->m_strName);
+        }
+
         return nullptr;
     }
-    return nullptr;
 }
 
 bool Thread_Base::StartThread(bool Sync, bool Inherit)
@@ -168,6 +205,10 @@ bool Thread_Base::StartThread(bool Sync, bool Inherit)
 
 bool Thread_Base::StopThread(unsigned long timeperiod) 
 {
+    // 如果已经停止,直接返回
+    if (!m_ThreadRunning) {
+        return true;
+    }
     bool ret = true;
     // 设置退出标志 - 通过信号量通知        
     m_ExitFlag->SetEvent();
@@ -212,32 +253,27 @@ bool Thread_Base::StopThread(unsigned long timeperiod)
 #endif
 
         if (joinResult == ETIMEDOUT) {
-            ERR_LOG("Timeout stopping thread " << m_strName);
-            // 尝试取消线程
-            pthread_cancel(m_Base_Thread);
-
-            // 尝试再次等待较短时间
-            clock_gettime(CLOCK_REALTIME, &ts);
-            ts.tv_sec += 5; // 额外等待5秒
-            ts.tv_nsec = 0;
-
-            if (pthread_timedjoin_np(m_Base_Thread, NULL, &ts) == 0) {
-                ERR_LOG("Failed to stop thread " << m_strName << ", detaching");
-                pthread_detach(m_Base_Thread);
-                ret = false;
-            }
-            // 执行线程退出清理
-            ThreadExitProcedure();
+            ERR_LOG("Timeout stopping thread " << m_strName << ", using soft detach");
+            // 不再使用 pthread_cancel,而是记录警告并分离
+            pthread_detach(m_Base_Thread);
+            ret = false;
+
+            // 即使分离,也要确保清理状态
+            m_ThreadRunning = false;
+            m_Base_Thread = 0;
+            m_ThreadID = 0;
         }
-        else if (joinResult != 0) {  
+        else if (joinResult != 0) {
             ERR_LOG("Failed to join thread " << m_strName << ", error: " << joinResult);
             ret = false;
         }
-        m_Base_Thread = 0;
+        else {
+            // 正常停止
+            m_Base_Thread = 0;
+            m_ThreadID = 0;
+            m_ThreadRunning = false;
+        }
     }
-
-    m_ThreadID = 0;
-    m_ThreadRunning = false;
     return ret;
 }
 
@@ -321,17 +357,23 @@ bool Thread_Base::WaitTheThreadEndSign(unsigned long waittime)
 
 bool Thread_Base::SetThreadOnTheRun(bool OnTheRun)
 {
-    if (OnTheRun)
-    {
-        m_RunFlag->SetEvent();
+    if (!m_ThreadRunning || !m_RunFlag) {
+        return false;
+    }
 
+    try {
+        if (OnTheRun) {
+            m_RunFlag->SetEvent();
+        }
+        else {
+            m_RunFlag->ResetEvent();
+        }
+        return true;
     }
-    else
-    {
-        m_RunFlag->ResetEvent();
+    catch (...) {
+        // 捕获所有异常,防止崩溃传播
+        return false;
     }
-
-    return true;
 }
 
 void Thread_Base::NotifyThreadWork()
@@ -371,11 +413,67 @@ std::shared_ptr<LinuxEvent> Thread_Base::GetExitEvt()
 }
 
 void Thread_Base::ThreadExitProcedure() {
-    m_ThreadRunning = false;
-    m_ExitFlag->SetEvent();
-    OnEndThread();
-    UnRegistThread();
-    SetThreadOnTheRun(false);
+    // 避免重复清理
+    if (!m_ThreadRunning) {
+        return;
+    }
+
+    try {
+        m_ThreadRunning = false;
+
+        // 安全设置事件
+        if (m_ExitFlag) {
+            try {
+                m_ExitFlag->SetEvent();
+            }
+            catch (const std::exception& e) {
+                ERR_LOG("[Thread] SetEvent exception in " << m_strName << ": " << e.what());
+            }
+            catch (...) {
+                ERR_LOG("[Thread] Unknown SetEvent exception in " << m_strName);
+            }
+        }
+
+        // 保护 OnEndThread - 允许失败但不影响清理
+        try {
+            OnEndThread();
+        }
+        catch (const std::exception& e) {
+            ERR_LOG("[Thread] OnEndThread exception in " << m_strName << ": " << e.what());
+        }
+        catch (...) {
+            ERR_LOG("[Thread] Unknown OnEndThread exception in " << m_strName);
+        }
+
+        // 保护 UnRegistThread - 允许失败但不影响清理
+        try {
+            UnRegistThread();
+        }
+        catch (const std::exception& e) {
+            ERR_LOG("[Thread] UnRegistThread exception in " << m_strName << ": " << e.what());
+        }
+        catch (...) {
+            ERR_LOG("[Thread] Unknown UnRegistThread exception in " << m_strName);
+        }
+
+        // 安全设置运行状态
+        try {
+            SetThreadOnTheRun(false);
+        }
+        catch (const std::exception& e) {
+            ERR_LOG("[Thread] SetThreadOnTheRun exception in " << m_strName << ": " << e.what());
+        }
+        catch (...) {
+            ERR_LOG("[Thread] Unknown SetThreadOnTheRun exception in " << m_strName);
+        }
+
+    }
+    catch (const std::exception& e) {
+        ERR_LOG("[Thread] Critical ThreadExitProcedure exception in " << m_strName << ": " << e.what());
+    }
+    catch (...) {
+        ERR_LOG("[Thread] Critical unknown ThreadExitProcedure exception in " << m_strName);
+    }
 }
 
 // Work_Thread 实现