亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問板塊 發(fā)新帖
查看: 1512 | 回復(fù): 3
打印 上一主題 下一主題

[Linux] 請(qǐng)教一個(gè)關(guān)于線程取消的問題 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2015-03-27 21:56 |只看該作者 |倒序?yàn)g覽
<The linux programming interface>

Listing 32-2: Using cleanup handlers
––––––––––––––––––––––––––– ––––––––––––––––––––––– threads/thread_cleanup.c
#include <pthread.h>
#include "tlpi_hdr.h"
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static int glob = 0;             /* Predicate variable */
static void     /* Free memory pointed to by 'arg' and unlock mutex */
cleanupHandler(void *arg)
{
    int s;
    printf("cleanup: freeing block at %p\n", arg);
    free(arg);
    printf("cleanup: unlocking mutex\n");
    s = pthread_mutex_unlock(&mtx);
    if (s != 0)
        errExitEN(s, "pthread_mutex_unlock");
}
static void *
threadFunc(void *arg)
{
    int s;
    void *buf = NULL;                   /* Buffer allocated by thread */
    buf = malloc(0x10000);              /* Not a cancellation point */
    printf("thread:  allocated memory at %p\n", buf);
    s = pthread_mutex_lock(&mtx);       /* Not a cancellation point */
    if (s != 0)
        errExitEN(s, "pthread_mutex_lock");
    pthread_cleanup_push(cleanupHandler, buf);
    while (glob == 0) {
        s = pthread_cond_wait(&cond, &mtx);    /* A cancellation point */
        if (s != 0)
            errExitEN(s, "pthread_cond_wait");
    }
    printf("thread:  condition wait loop completed\n");
    pthread_cleanup_pop(1);             /* Executes cleanup handler */
    return NULL;
}
int
main(int argc, char *argv[])
{
    pthread_t thr;
    void *res;
    int s;
    s = pthread_create(&thr, NULL, threadFunc, NULL);
    if (s != 0)
        errExitEN(s, "pthread_create");
    sleep(2);                   /* Give thread a chance to get started */
    if (argc == 1) {            /* Cancel thread */
        printf("main:    about to cancel thread\n");
        s = pthread_cancel(thr);
        if (s != 0)
            errExitEN(s, "pthread_cancel");
    } else {                    /* Signal condition variable */
        printf("main:    about to signal condition variable\n");
        glob = 1;
        s = pthread_cond_signal(&cond);
        if (s != 0)
            errExitEN(s, "pthread_cond_signal");
    }
    s = pthread_join(thr, &res);
    if (s != 0)
        errExitEN(s, "pthread_join");
    if (res == PTHREAD_CANCELED)
        printf("main:    thread was canceled\n");
    else
        printf("main:    thread terminated normally\n");
    exit(EXIT_SUCCESS);
}

這段程序中, 在s = pthread_cond_wait(&cond, &mtx);    /* A cancellation point */時(shí)發(fā)生線程取消的話, 為何需要在處理函數(shù)cleanupHandler中對(duì)互斥量mtx進(jìn)行解鎖,
pthread_cond_wait(&cond, &mtx)在等待的時(shí)候不是已經(jīng)解鎖了mtk了么

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
2 [報(bào)告]
發(fā)表于 2015-03-30 13:30 |只看該作者
本帖最后由 羽劍天涯 于 2015-03-30 15:46 編輯

回復(fù) 1# qiumupo


    pthread_cond_wait這個(gè)函數(shù)調(diào)用后,會(huì)解鎖傳遞的mutex,同時(shí)進(jìn)入阻塞,等待傳遞的cond,(此時(shí)mutex可以被別的線程加鎖,并通知cond),當(dāng)cond通知收到后,會(huì)嘗試重新去加鎖,加鎖成功后,該函數(shù)會(huì)返回,(此時(shí)mutex重新被當(dāng)前線程加鎖),也就是在該函數(shù)調(diào)用前和調(diào)用后,mutex都是本線程加鎖狀態(tài),在該函數(shù)調(diào)用中,mutex才會(huì)處于解鎖狀態(tài)。
    另外,調(diào)用pthread_cancel時(shí),如未特別指定,取消狀態(tài)為PTHREAD_CANCEL_ENABLE,取消類型為PTHREAD_CANCEL_DEFERED,也就是執(zhí)行了下述函數(shù)之一后采取行動(dòng):pthread_join,pthread_cond_wait,pthread_cond_timedwait,pthread_testcancel,sem_wait或sigwait。從你的線程代碼中可以看到,是在pthread_con_wait結(jié)束后行動(dòng),此時(shí)已經(jīng)加鎖了。
    另外該線程在自己退出時(shí)也是調(diào)用cleanupHandler,這樣也需要進(jìn)行free和解鎖。

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2015-04-13 23:49 |只看該作者
回復(fù) 2# 羽劍天涯


    是不是設(shè)置下線程分離比較好。pthread_detach().  這樣線程結(jié)束時(shí)自動(dòng)回收資源。。

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
4 [報(bào)告]
發(fā)表于 2015-04-14 17:13 |只看該作者
回復(fù) 3# guojinshuai


    pthread_detach只是將線程設(shè)置為不需要join,回收的僅僅是線程的退出狀態(tài)等一些資源,鎖、條件變量等其他資源并不會(huì)自動(dòng)回收(這些資源也沒有直接屬于該線程)……
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP