C++多線程強制終止詳細
前言:
故事的起因來源于我在優(yōu)化他人c++源碼的時候,想通過多線程的方式提升程序的運算效率,主要存在以下需求和難點:
- 多個線程并行跑模型,看哪個模型跑的快,跑出來后結(jié)束其他線程,線程間獨立運行無通信過程
- 源碼模型很復雜,函數(shù)調(diào)用較多,不好改動,因此不太適合通過信號或標志進行通信終止
線程結(jié)束的幾種方式:
線程函數(shù)的return返回(建議):這種退出線程的方式是最安全的,在線程函數(shù)return返回后, 會清理函數(shù)內(nèi)申請的類對象, 即調(diào)用這些對象的析構(gòu)函數(shù).。然后會自動調(diào)用 _endthreadex()函數(shù)來清理 _beginthreadex()函數(shù)申請的資源(主要是創(chuàng)建的tiddata對象)。
同一個進程或另一個進程中的線程調(diào)用TerminateThread函數(shù)(應避免使用該方法):TerminateThread能夠撤消任何線程,其中hThread參數(shù)用于標識被終止運行的線程的句柄。當線程終止運行時,它的退出代碼成為你作為dwExitCode參數(shù)傳遞的值。同時,線程的內(nèi)核對象的使用計數(shù)也被遞減。注意TerminateThread函數(shù)是異步運行的函數(shù),也就是說,它告訴系統(tǒng)你想要線程終止運行,但是,當函數(shù)返回時,不能保證線程被撤消。如果需要確切地知道該線程已經(jīng)終止運行,必須調(diào)用WaitForSingleObject或者類似的函數(shù),傳遞線程的句柄。
通過調(diào)用ExitThread函數(shù):線程將自行撤消(最好不使用該方法)。該函數(shù)將終止線程的運行,并導致操作系統(tǒng)清除該線程使用的所有操作系統(tǒng)資源。但是,C++資源(如C++類對象)將不被析構(gòu)。
ExitProcess和TerminateProcess函數(shù)也可以用來終止線程的運行(應避免使用該方法):
選項2和3可能會導致內(nèi)存泄漏,實際上,沒有任何語言或操作系統(tǒng)可以為你提供異步突然終止線程的便利,且不會警告你不要使用它們。所有這些執(zhí)行環(huán)境都強烈建議開發(fā)人員,甚至要求在協(xié)作或同步線程終止的基礎上構(gòu)建多線程應用程序。
現(xiàn)有的線程結(jié)束函數(shù),包括linux系統(tǒng)的pthread.h中的pthread_exit()和pthread_cancel(),windows系統(tǒng)的win32.h中的ExitThread()和TerminateThread(),也就是說,C++沒有提供kill掉某個線程的能力,只能被動地等待某個線程的自然結(jié)束,析構(gòu)函數(shù)~thread()也不能停止線程,析構(gòu)函數(shù)只能在線程靜止時終止線程joinable,對于連接/分離的線程,析構(gòu)函數(shù)根本無法終止線程。
要終止與OS /編譯器相關(guān)的函數(shù)的線程,我們需要知道如何從C++獲取本機線程數(shù)據(jù)類型std::thread。幸運的是,在調(diào)用或之前std::thread提供了一個API native_handle()以獲取線程的本機句柄類型。并且可以將此本地句柄傳遞給本地OS線程終止函數(shù),例如join() detach() pthread_cancel()。
以下代碼用于顯示std::thread::native_handle() ,std::thread::get_id()并pthread_self()返回相同的代碼pthread_t來處理Linux / GCC的C++線程
#include <mutex>
#include <iostream>
#include <chrono>
#include <cstring>
#include <pthread.h>
std::mutex iomutex;
void f(int num)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::lock_guard<std::mutex> lk(iomutex);
std::cout << "Thread " << num << " pthread_t " << pthread_self() << std::endl;
}
int main()
{
std::thread t1(f, 1), t2(f, 2);
//t1.join(); t2.join(); ----------------pos 1
//t1.detach(); t2.detach(); -------------pos 2
std::cout << "Thread 1 thread id " << t1.get_id() << std::endl;
std::cout << "Thread 2 thread id " << t2.get_id() << std::endl;
std::cout << "Thread 1 native handle " << t1.native_handle() << std::endl;
std::cout << "Thread 2 native handle " << t2.native_handle() << std::endl;
t1.join(); t2.join();
//t1.detach(); t2.detach();
}
運行后可以得到結(jié)果
$ 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
uncommentpos 1或者pos 2后,即調(diào)用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
因此,要有效地調(diào)用本機線程終止函數(shù)(例如pthread_cancel),需要在調(diào)用std::thread::join()時或之前保存本機句柄std::thread::detach()。這樣,始終可以使用有效的本機句柄終止線程。
class Foo {
public:
void sleep_for(const std::string &tname, int num)
{
prctl(PR_SET_NAME,tname.c_str(),0,0,0);
sleep(num);
}
void start_thread(const std::string &tname)
{
std::thread thrd = std::thread(&Foo::sleep_for, this, tname, 3600);
tm_[tname] = thrd.native_handle();
thrd.detach();
std::cout << "Thread " << tname << " created:" << std::endl;
}
void stop_thread(const std::string &tname)
{
ThreadMap::const_iterator it = tm_.find(tname);
if (it != tm_.end()) {
pthread_cancel(it->second);
tm_.erase(tname);
std::cout << "Thread " << tname << " killed:" << std::endl;
}
}
private:
typedef std::unordered_map<std::string, pthread_t> ThreadMap;
ThreadMap tm_;
};
int main()
{
Foo foo;
std::string keyword("test_thread");
std::string tname1 = keyword + "1";
std::string tname2 = keyword + "2";
// create and kill thread 1
foo.start_thread(tname1);
foo.stop_thread(tname1);
// create and kill thread 2
foo.start_thread(tname2);
foo.stop_thread(tname2);
return 0;
}
結(jié)果是:
$ g++ -Wall -std=c++11 kill_cpp_thread.cc -o kill_cpp_thread -pthread -lpthread
$ ./kill_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:
當然,條件允許的話最好還是使用返回或信號的方式終止線程,這樣也符合安全可信的要求。
到此這篇關(guān)于C++多線程強制終止詳細的文章就介紹到這了,更多相關(guān)C++多線程強制終止內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++可變參數(shù)函數(shù)的實現(xiàn)方法示例
這篇文章主要給大家介紹了關(guān)于C++可變參數(shù)函數(shù)的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12

