欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
關(guān)于 pthread

關(guān)于 pthread_cleanup_push 有如下描述:
================================
從pthread_cleanup_push()的調用點(diǎn)到pthread_cleanup_pop()之間的程序段中的終止動(dòng)作
(包括調用pthread_exit()和取消點(diǎn)終止)都將執行pthread_cleanup_push()所指定的清理函數。
================================
請問(wèn)其中的“取消點(diǎn)終止”是什么意思?
我嘗試了以下的 pthread_cancel(),但并不會(huì )觸發(fā) clean(),這是為什么?


以下是代碼:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>

void clean(void* p)
{
        printf("clean()\n");
}

void* thread(void* p)
{
        pthread_cleanup_push(clean, NULL);
        //===============================
        printf("thread sleep\n");
        sleep(10);
        printf("thread wake...\n");
        //===============================
        pthread_cleanup_pop(1);
}

int main()
{
        pthread_t        id;
        pthread_create(&id, NULL, thread, NULL);
        printf("main sleep\n");
        sleep(3);
        printf("cancel thread: %u\n", id);
        pthread_cancel(id);
        return 0;
}

==========================
輸出:
thread sleep
main sleep
cancel thread: 3085872016
==========================

在 xxx_push() 和 xxx_pop() 之間唯有調用 pthread_exit() 會(huì )觸發(fā) clean() 函數,
return 也不會(huì ),(異常沒(méi)有試過(guò), 還不會(huì )手動(dòng)拋出異常);


這是一個(gè)老問(wèn)題了,也看到別人問(wèn)過(guò)(比如: http://bbs.chinaunix.net/viewthread.php?tid=456153)
但在網(wǎng)上一直沒(méi)有找到正式的回答;




回復 #1 chengdot 的帖子

我的理解是 pthread_cancel() 應該在thread函數中調用,
pthread_cleanup_push() installs the cancellation cleanup handler
      routine onto the calling thread's cancellation cleanup stack. This
      handler will be popped from the calling thread's cancellation cleanup
      stack。







本帖最后由 new_new_one 于 2010-09-15 09:26 編輯

頂起來(lái)。

首先你必須知道pthread_cleanup_push與pthread_cleanup_pop的目的(作用)是什么。

比如thread1:
執行
pthread_mutex_lock(&mutex);

//一些會(huì )阻塞程序運行的調用,比如套接字的accept,等待客戶(hù)連接
sock = accept(......);            //這里是隨便找的一個(gè)可以阻塞的接口

pthread_mutex_unlock(&mutex);
這個(gè)例子中,如果線(xiàn)程1執行accept時(shí),線(xiàn)程會(huì )阻塞(也就是等在那里,有客戶(hù)端連接的時(shí)候才返回,或則出現其他故障),線(xiàn)程等待中......

這時(shí)候線(xiàn)程2發(fā)現線(xiàn)程1等了很久,不賴(lài)煩了,他想關(guān)掉線(xiàn)程1,于是調用pthread_cancel()或者類(lèi)似函數,請求線(xiàn)程1立即退出。

這時(shí)候線(xiàn)程1仍然在accept等待中,當它收到線(xiàn)程2的cancel信號后,就會(huì )從accept中退出,然后終止線(xiàn)程,注意這個(gè)時(shí)候線(xiàn)程1還沒(méi)有執行:
pthread_mutex_unlock(&mutex);
也就是說(shuō)鎖資源沒(méi)有釋放,這回造成其他線(xiàn)程的死鎖問(wèn)題。

所以必須在線(xiàn)程接收到cancel后用一種方法來(lái)保證異常退出(也就是線(xiàn)程沒(méi)達到終點(diǎn))時(shí)可以做清理工作(主要是解鎖方 面),pthread_cleanup_push與pthread_cleanup_pop就是這樣的。

pthread_cleanup_push(some_clean_func,...)
pthread_mutex_lock(&mutex);

//一些會(huì )阻塞程序運行的調用,比如套接字的accept,等待客戶(hù)連接
sock = accept(......);            //這里是隨便找的一個(gè)可以阻塞的接口

pthread_mutex_unlock(&mutex);
pthread_cleanup_pop(0);
return NULL;
上面的代碼,如果accept被cancel后線(xiàn)程退出,會(huì )自動(dòng)調用some_clean_func函數,在這個(gè)函數中你可以釋放鎖資源。如果 accept沒(méi)有被cancel,那么線(xiàn)程繼續執行,當pthread_mutex_unlock(&mutex);表示線(xiàn)程自己正確的釋放資源 了,而執行pthread_cleanup_pop(0);也就是取消掉前面的some_clean_func函數。接著(zhù)return線(xiàn)程就正確的結束 了。

不曉得你明白沒(méi),通俗點(diǎn)就是:
pthread_cleanup_push注冊一個(gè)回調函數,如果你的線(xiàn)程在對應的pthread_cleanup_pop之前異常退出(return是 正常退出,其他是異常),那么系統就會(huì )執行這個(gè)回調函數(回調函數要做什么你自己決定)。但是如果在pthread_cleanup_pop之前沒(méi)有異常 退出,pthread_cleanup_pop就把對應的回調函數取消了,

關(guān)于取消點(diǎn)的解釋?zhuān)?br>
比如你執行:
        printf("thread sleep\n");
        sleep(10);
        printf("thread wake...\n");
在sleep函數中,線(xiàn)程睡眠,結果收到cancel信號,這時(shí)候線(xiàn)程從sleep中醒來(lái),但是線(xiàn)程不會(huì )立刻退出。這是應為pthread與C庫方面的原 因(具體是啥我也不清楚),pthread的建議是,如果一個(gè)函數是阻塞的,那么你必須在這個(gè)函數前 后建立取消點(diǎn),比如:
        printf("thread sleep\n");
        pthread_testcancel();
        sleep(10);
        pthread_testcancel();
        printf("thread wake...\n");
這樣,就添加了兩個(gè)取消掉。在執行到pthread_testcancel的位置時(shí),線(xiàn)程才可能響應cancel退出進(jìn)程。

額外的知識:
對于cancel信號,線(xiàn)程有兩種方法: 忽略,和響應。默認是響應
接收到cancel信號,線(xiàn)程有兩種處理類(lèi)型: 立即響應 和 延遲響應(在最近的取消點(diǎn)響應),默認是延遲響應




至于你的代碼例子中為什么沒(méi)有看到:
printf("clean()\n");
這句的答應結果,是因為你的主線(xiàn)程在執行pthread_cancel后就退出了,這導致進(jìn)程退出。
換句話(huà)說(shuō)就是你的:printf("clean()\n");還沒(méi)來(lái)得及執行,進(jìn)程就釋放了資源。

你在執行pthread_cancel后在加上一個(gè)sleep就行了

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
pthread編程基礎
pthread_clean_push和pthread_clean_up
Posix線(xiàn)程編程指南
線(xiàn)程終止thread_exit,pthread_cancel,pthread_join
epoll_wait是線(xiàn)程退出點(diǎn)嗎? - General - 豆花魚(yú)片加工室
POSIX線(xiàn)程庫API(全)(下)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久