日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ 多线程——pthread_cancel 取消线程的疑惑

發布時間:2023/12/20 c/c++ 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ 多线程——pthread_cancel 取消线程的疑惑 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C++ 多線程——pthread_cancel 取消線程的疑惑

測試環境:Ubuntu18.04

pthread_cancel 簡介

pthread_cancel(threadID)會發送終止信號給thread線程,如果成功則返回0,否則為非0值。pthread_cancel調用并不等待線程終止,它只是向目標線程發Cancel信號, 提出取消請求。但目標線程如何處理Cancel信號則由目標線程自己決定,或者忽略(當禁止取消時)、或者立即終止(當在取消點或異步模式下)、或者繼續運行至Cancelation-point(取消點,下面將描述),總之由不同的Cancelation狀態決定。

示例代碼

下面這個示例代碼將開啟兩個子線程thread1 thread2

thread1 —> while死循環輸出信息

thread2 —> 延時兩秒 然后通過pthread_cancel來取消thread1線程

代碼如下:

#include <iostream> #include <unistd.h> #include <pthread.h>void* thread1(void *arg) {while (1){std::cout << "thread1 running" << std::endl;}std::cout << "Leave thread1!" << std::endl;return nullptr; }void* thread2(void *arg) {sleep(2);pthread_cancel(*static_cast<pthread_t*>(arg)); //取消線程std::cout << "send pthread_cancel!" << std::endl;return nullptr; }int main(int argc, char** argv) {// 啟動子線程1 join運行pthread_t tid1;pthread_create(&tid1, nullptr, thread1, nullptr);// 啟動子線程2 detach運行 計時兩秒后 發送pthread_cancel 來結束 子線程1pthread_t tid2;pthread_create(&tid2, nullptr, thread2, &tid1);// pthread_detach()即主線程與子線程分離,兩者相互不干涉,不會阻塞主線程,主線程結束同時子線程的資源自動回收pthread_detach(tid2);// pthread_join()即是子線程合入主線程,主線程會一直阻塞,直到子線程執行結束,然后回收子線程資源,并繼續向下執行pthread_join(tid1, nullptr);// 上述代碼 由于thread1 會阻塞主線程,所以程序會阻塞在這里無法繼續執行。當成功取消thread1后會繼續向下執行。這里用打印 "main thread!" 來查看 thread1 是否結束。std::cout << "main thread!" << std::endl;while (1) {sleep(1);}return 0; }

運行結果

........ thread1 running thread1 running thread1 running send pthread_cancel!

可以看到程序并沒有打印 main thread! 說明 thread1 子線程并沒有結束。

難道是因為 線程處于無限循環中,且循環體內沒有執行至取消點的必然路徑,則線程無法由外部其他線程的取消請求而終止?????

這種問題該怎么解決呢?

解決方法一 手動插入一個取消點

void* thread1(void *arg) {while (1){std::cout << "thread1 running" << std::endl;/** 手動創建一個取消點 pthread_testcancel()* pthread_testcancel會檢查本線程是否處于Canceld狀態,如果是,則進行取消動作,否則直接返回。 * 此函數在線程內執行,執行的位置就是線程退出的位置,在執行此函數以前,線程內部的相關資源申請一定要釋放掉,他很容易造成內存泄露。*/pthread_testcancel();}std::cout << "Leave thread1!" << std::endl;return nullptr; }

運行結果

........ thread1 running thread1 running thread1 running send pthread_cancel! main thread!

可以看到thread1成功取消

解決方法二 sleep 延時

void* thread1(void *arg) {while (1){std::cout << "thread1 running" << std::endl;/** usleep(1) 延時1微妙 也就是0.001毫秒*/usleep(1);}std::cout << "Leave thread1!" << std::endl;return nullptr; }

運行結果

........ thread1 running thread1 running thread1 running send pthread_cancel! main thread!

哇哦,也成功了…

難道是while死循環中沒有延時 導致cancel信號被忽略??? 還是因為 sleep 延時本身就算是一個取消點???

再改變一下log輸出的方式

void* thread1(void *arg) {while (1){// std::cout << "thread1 running" << std::endl;printf("thread1 running\n");}std::cout << "Leave thread1!" << std::endl;return nullptr; }

運行結果

........ thread1 running thread1 running thread1 running send pthread_cancel! main thread!

這到底怎么回事??
只是簡單的換了一下log的輸出方式,就產生了不一樣的結果。
作為小白,我也很疑惑…

pthread 的高級配置

設置本線程對Cancel信號的反應 int pthread_setcancelstate(int state, int *oldstate); state有兩種值:PTHREAD_CANCEL_ENABLE(缺省)和PTHREAD_CANCEL_DISABLE 分別表示收到信號后設為CANCLED狀態和忽略CANCEL信號繼續運行;old_state如果不為NULL則存入原來的Cancel狀態以便恢復。設置本線程取消動作的執行時機 int pthread_setcanceltype(int type, int *oldtype); type由兩種取值:PTHREAD_CANCEL_DEFFERED和PTHREAD_CANCEL_ASYCHRONOUS 僅當Cancel狀態為Enable時有效,分別表示收到信號后繼續運行至下一個取消點再退出和立即執行取消動作(退出);oldtype如果不為NULL則存入運來的取消動作類型值。

但測試感覺一點效果沒有,可能是我使用方式不對,代碼如下:

void* thread1(void *arg) {pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); //允許退出線程pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); //設置立即取消while (1){std::cout << "thread1 running" << std::endl;}std::cout << "Leave thread1!" << std::endl;return nullptr; }

運行結果

........ thread1 running thread1 running thread1 running send pthread_cancel!

參考博文

https://www.cnblogs.com/lijunamneg/archive/2013/01/25/2877211.html

https://www.cnblogs.com/wangchaoguo-li/archive/2012/11/08/2760006.html

總結

以上是生活随笔為你收集整理的C++ 多线程——pthread_cancel 取消线程的疑惑的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。