• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            小默

            thread 取消線程

            取消一個(gè)線程

            可以請(qǐng)求一個(gè)線程終止,像給它發(fā)送一個(gè)信號(hào)一樣.

            1 請(qǐng)求端:

            #include <pthread.h>
            int pthread_cancel(pthread_t thread);
            請(qǐng)求取消指定線程.

            2 接收請(qǐng)求端:

            #include <pthread.h>
            int pthread_setcancelstate(int state, int *oldstate);
            state: 線程是否接收取消請(qǐng)求.
            | PTHREAD_CANCEL_ENABLE 允許線程接收取消請(qǐng)求
            | PTHREAD_CANCEL_DISABLE 忽略取消請(qǐng)求
            oldstate: 獲取先前的取消狀態(tài),不關(guān)心先前狀態(tài)傳NULL.

            #include <pthread.h>
            int pthread_setcanceltype(int type, int *oldtype);
            type: 如果接收到取消請(qǐng)求,什么時(shí)候采取行動(dòng).
            | PTHREAD_CANCEL_ASYNCHRONOUS 接收到取消請(qǐng)求后立即采取行動(dòng).
            | PTHREAD_CANCEL_DEFERRED 接收到取消請(qǐng)求后,等待請(qǐng)求端線程執(zhí)行以下函數(shù)之一,再采取行動(dòng).
            |    (pthread_join,pthread_cond_wait, pthread_cond_timedwait, pthread_testcancel, sem_wait, sigwait)
            oldtype: 獲取先前的取消狀態(tài),不關(guān)心先前狀態(tài)傳NULL.

            取消一個(gè)線程:
            /*
             * 取消一個(gè)線程
             
            */
            #include 
            <stdio.h>
            #include 
            <unistd.h>
            #include 
            <stdlib.h>
            #include 
            <pthread.h>

            void *thread_function(void *arg);

            int main(){
                
            int res;
                pthread_t a_thread;
                
            void *thread_result;

                res 
            = pthread_create(&a_thread, NULL, thread_function, NULL);
                
            if(res != 0){
                    perror(
            "Thread creation failed");
                    exit(EXIT_FAILURE);
                }

                sleep(
            3);
                printf(
            "Canceling thread\n");
                res 
            = pthread_cancel(a_thread);
                
            if(res != 0){
                    perror(
            "Thread cancelation failed");
                    exit(EXIT_FAILURE);
                }

                printf(
            "Waiting for thread to finished\n");
                res 
            = pthread_join(a_thread, &thread_result);
                
            if(res != 0){
                    perror(
            "Thread join failed");
                    exit(EXIT_FAILURE);
                }

                exit(EXIT_SUCCESS);
            }

            void *thread_function(void *arg){
                
            int i,res;

                res 
            = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
                
            if(res != 0){
                    perror(
            "Thread pthread_setcancelstate failed");
                    exit(EXIT_FAILURE);
                }

                res 
            = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
                
            if(res != 0){
                    perror(
            "Thread pthread_setcanceltype failed");
                    exit(EXIT_FAILURE);
                }
                printf(
            "thread_function is running\n");

                
            for(i = 0; i < 10; i++){
                    printf(
            "Thread is still running (%d)\n", i);
                    sleep(
            1);
                }

                pthread_exit(
            0);
            }

            運(yùn)行結(jié)果:
            $ gcc -D_REENTRANT thread7.c -o thread7 -lpthread
            $ .
            /thread7
            thread_function 
            is running
            Thread 
            is still running (0)
            Thread 
            is still running (1)
            Thread 
            is still running (2)
            Canceling thread
            Waiting 
            for thread to finished

            --
            FROM:Linux程序設(shè)計(jì)

            posted on 2011-06-14 19:45 小默 閱讀(676) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Linux

            導(dǎo)航

            統(tǒng)計(jì)

            留言簿(13)

            隨筆分類(287)

            隨筆檔案(289)

            漏洞

            搜索

            積分與排名

            最新評(píng)論

            閱讀排行榜

            亚洲а∨天堂久久精品| 久久精品国产亚洲AV电影| 久久国产色AV免费观看| 少妇久久久久久被弄到高潮| 韩国三级中文字幕hd久久精品| 国内精品伊人久久久久| 久久综合综合久久狠狠狠97色88| 波多野结衣中文字幕久久| 久久精品无码专区免费东京热| 久久久久久人妻无码| 久久人人爽人人爽人人片AV不 | …久久精品99久久香蕉国产| 久久久久久曰本AV免费免费| 久久亚洲精品成人无码网站| 欧美日韩精品久久久久| 国产国产成人久久精品| 久久久久久久久久免免费精品| 综合久久一区二区三区| 亚洲AV伊人久久青青草原| 久久亚洲精品人成综合网| 精品久久久久久无码中文字幕一区| 久久久www免费人成精品| 久久久久亚洲AV无码麻豆| 99久久综合狠狠综合久久| 久久久久久久免费视频| 日韩人妻无码一区二区三区久久 | 91精品婷婷国产综合久久| 成人亚洲欧美久久久久 | 久久久这里只有精品加勒比| 亚洲va国产va天堂va久久| 国产高潮国产高潮久久久91 | 色婷婷综合久久久中文字幕| 一本久久a久久精品综合香蕉| 久久99精品国产99久久6| 伊人久久大香线蕉影院95| 久久精品国产99国产精偷| 精品久久777| 久久亚洲国产精品一区二区| 精品久久久久久亚洲| 中文精品久久久久国产网址| 777久久精品一区二区三区无码|