• <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 取消線程

            取消一個線程

            可以請求一個線程終止,像給它發(fā)送一個信號一樣.

            1 請求端:

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

            2 接收請求端:

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

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

            取消一個線程:
            /*
             * 取消一個線程
             
            */
            #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);
            }

            運行結果:
            $ 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程序設計

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

            導航

            統(tǒng)計

            留言簿(13)

            隨筆分類(287)

            隨筆檔案(289)

            漏洞

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            久久久老熟女一区二区三区| 久久99精品国产麻豆不卡| 99久久精品国产一区二区| 久久久久亚洲av无码专区| 色综合色天天久久婷婷基地| 99精品国产免费久久久久久下载| 伊人久久精品无码二区麻豆| 精品人妻伦一二三区久久| 亚洲国产精品无码久久久不卡| 99久久国产综合精品网成人影院 | 欧美日韩精品久久久免费观看| 日产精品久久久久久久性色| 久久精品国产清自在天天线| 久久精品中文字幕无码绿巨人| 伊人 久久 精品| 久久精品亚洲乱码伦伦中文 | 国产福利电影一区二区三区久久老子无码午夜伦不 | 大伊人青草狠狠久久| 久久精品国产WWW456C0M| 青青青青久久精品国产h久久精品五福影院1421 | 久久久国产一区二区三区| 成人午夜精品无码区久久 | AAA级久久久精品无码区| 久久发布国产伦子伦精品| 久久青青草视频| 久久久无码精品亚洲日韩软件| 久久免费国产精品一区二区| 国内精品九九久久久精品| 久久综合国产乱子伦精品免费| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 国产L精品国产亚洲区久久| 久久午夜无码鲁丝片秋霞| 久久综合色区| 久久大香萑太香蕉av| 亚洲成av人片不卡无码久久| 久久久久久A亚洲欧洲AV冫| 久久se这里只有精品| 日韩久久无码免费毛片软件| 三级片免费观看久久| 久久久久久国产精品无码下载 | 久久99国产精品99久久|