C 多執行緒強制終止詳細

2022-09-24 12:45:13 字數 3069 閱讀 1327

目錄

前言:故事的起因**於我在優化他人c++原始碼的時候,想通過多執行緒的方式提公升程式的運算效率,主要存在以下需求和難點:

執行緒結束的幾種方式:

執行緒函式的return返回(建議):這種退出執行緒的方式是最安全的,**程函式return返回後, 會清理函式內申請的類物件, 即呼叫這些物件的析構函式.。然後會自動呼叫 _endthreadex()函式來清理_beginthreadex()函式申請的資源(主要是建立的tiddata物件)。

同乙個程序或另乙個程序中的執行緒呼叫terminatethread函式(應避免使用該方法):terminatethread能夠撤消任何執行緒,其中hthread引數用於標識被終止執行的執行緒的控制代碼。當執行緒終止執行時,它的退出**成為你作為dwexitcode引數傳遞的值。同時,執行緒的核心物件的使用計數也被遞減。注意terminatethread函式是非同步執行的函式,也就是說,它告訴系統你想要執行緒終止執行,但是,當函式返回時,不能保證執行緒被撤消。如果需要確切地知道該執行緒已經終止執行,必須呼叫waitforsingleobject或者類似的函式,傳遞執行緒的控制代碼。

通過呼叫exitthread函式:執行緒將自行撤消(最好不使用該方法)。該函式將終止執行緒的執行,並導致作業系統清除該執行緒使用的所有作業系統資源。但是,c++資源(如c++類物件)將不被析構。

exitprocess和terminateprocess函式也可以用來終止執行緒的執行(應避免使用該方法):

選項2程式設計客棧和3可能會導致記憶體洩漏,實際上,沒有任何語言或作業系統可以為你提供非同步突然終止執行緒的便利,且不會警告你不要使用它們。所有這些執行環境都強烈建議開發人員,甚至要求在協作或同步執行緒終止的基礎上構建多執行緒應用程式。

現有的執行緒結束函式,包括linux系統的pthread.h中的pthread_exit()和pthread_cancel(),windows系統的win32.h中的exitthread()和terminatethread(),也就是說,c++沒有提供kill掉某個執行緒的能力,只能被動地等待某個執行緒的自然結束,析構函式~thread()也不能停止執行緒,析構函式只能**程靜止時終止執行緒joinable,對於連線/分離的執行緒,析構函式根本無法終止執行緒。

要終止與os /編譯器相關的函式的執行緒,我們需要知道如何從c++獲取本機執行緒資料型別std::thread。幸運的是,在呼叫或之前std::thread提供了乙個api native_handle()以獲取執行緒的本機控制代碼型別。並且可以將此本地控制代碼傳遞給本地os執行緒終止函式,例如join() detach() pthread_cancel()。

以下**用於顯示std::thread::native_handle(),std::thread::get_id()並pthread_self()返回相同的**pthread_t來處理linux / gcc的c++執行緒

#include

#include

#include

#include

#include

std::mutex iomutex;

void f(int num)

int main()

執行後可以得到結果

$ g++ -wall -std=c++11 cpp_thread_pthread.cc -o cpp_thread_pthread -pthread -lpthread

$ ./cpp_thread_pthread

thread 1 thread id 140109390030592

thread 2 thread id 140109381637888

thread 1 native handle 140109390030592

thread 2 native handle 140109381637888

thread 1 pthread_t 140109390030592

thread 2 pthread_t 140109381637888

uncommentpos1或者pos 2後,即呼叫join()或之後detach(),c++執行緒會丟失本機控制代碼型別的資訊

$ ./cpp_thread_pthread

thread 1 pthread_t 139811504355072

thread 2 pthread_t 139811495962368

thread 1 thread id thread::id of a non-executing thread

thread 2 thread id thread::id of a non-executing thread

thread 1 native handle 0

thread 2 native handle 0

因此,要有效地呼叫本機執行緒終止函式(例如pthread_cancel),需要在呼叫std::thread::join()時或之前儲存本機控制代碼std::thread::detach()。這樣,始終可以使用有效的本機控制代碼終止執行緒。

class foo

void start_thread(const std::string &tname)

void stop_thread(const std::string &tname)

}private:

typedef std::unordered_map<:string pthread_t> threadmap;

threadmap tm_;

};int main()

結果是:

$ g++ -wall -std=c++11 kill_cpp_thread.cc -owww.cppcns.com kill_cpp_thread -pthread -lpthread

$ ./ki程式設計客棧ll_cpp_thread

thread test_thread1 created:

30332 30333 pts/5    00:00:00 test_thread1

thread test_thread1 killed:

thread test_thread2 created:

30332 30340 pts/5    00:00:00 test_thread2

thread test_thread2 killed:

當然,條件允許的話最好還是使用返回或訊號的方式終止執行緒,這樣也符合安全可信的要求。

多執行緒 執行緒終止

stop 中止執行緒,並且清除監視器鎖的資訊,可能導致執行緒安全問題。destroy 從未實現過這個方法 public class demo thread.print public class stopthread extends thread catch interruptedexception ...

多執行緒 08 終止執行緒

執行緒的停止有兩種方式 第一種方式 執行緒體自己執行完畢,比如說裡面有個迴圈,或者就幾行 或者呼叫方法,呼叫完了就結束了。這是正常執行完畢。第二種方式 想法設法停止那些它不能自己停止的執行緒,一般來說就是那些沒有次數或者死迴圈,在jdk裡面提供了stop方法,但已經被過時,被jdk廢棄,替換為 只是...

C 多執行緒之旅 7 終止執行緒

先交代下背景,寫 c 多執行緒之旅 這個系列文章主要是因為以下幾個原因 1.多執行緒在c s和b s架構中用得是非常多的 2.而且多執行緒的使用是非常複雜的,如果沒有用好,容易造成很多問題。c 多執行緒之旅目錄 c 多執行緒之旅 1 介紹和基本概念 c 多執行緒之旅 2 建立和開始執行緒 c 多執行...