簡體   English   中英

停止線程期間的pthread_exit用法

[英]pthread_exit usage during stopping the thread

我正在創建一個像

pthread_create(&mon_thread, NULL, &ClassA::m_thread, this);

運行以下功能

void* ClassA::m_thread(void *arg){

  while (!halt_tx) {
  .....}
}

在停止期間,我設置halt_tx = 1並讓線程到達函數的末尾,在析構函數中,我調用join函數

ClassA::~ClassA()
{
   pthread_join(monitor_thread, NULL);
}

我的問題是在停止線程時是否還應該調用pthread_exit(NULL)。

沒有。

ClassA::m_thread函數結束時,將使用函數的返回值作為線程退出狀態對pthread_exit進行隱式調用。

但是請確保有正確的return語句。

pthread_exit的手冊頁顯示:

 Performing a return from the start function of any thread other than the main thread results in an implicit call to pthread_exit()

對於計算機而言,創建線程非常耗費資源。 在類中隱式創建線程,而至少沒有類的名稱,這會使線程創建非常清晰,這是一個壞主意。 對於我正在處理的項目,我有一個ConsumerThread類,該類由想要成為使用者的類繼承,該類將啟動線程並設置隊列。 開發人員不會實例化一個名為ConsumerThread的類,然后在創建線程時會感到驚訝。 但是,開發人員可以創建ClassA並且不知道創建了線程。 即使在編寫測試代碼時,我也要非常小心,以確保借助類名可以明顯地創建線程,以防在弱點時該類泄漏到生產環境中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM