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 來結束 子線程1
    pthread_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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章