• <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>

            Prayer

            在一般中尋求卓越
            posts - 1256, comments - 190, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            線程退出總結

            Posted on 2009-08-05 14:25 Prayer 閱讀(439) 評論(0)  編輯 收藏 引用 所屬分類: LINUX/UNIX/AIX

            #include <stdio.h>
            #include <stdlib.h>
            #include <string.h>
            #include <pthread.h>

            #define THREAD_NUM  3
            pthread_t threadBuf[THREAD_NUM];
            int       tcBuf[THREAD_NUM];

            void* threadOpr1(void* arg);
            void* threadOpr2(void* arg);

            /**********************************
            試驗一個線程殺死其他的線程
            但是進程不退出
            ***********************************/
            int main(){
               int i=0;
              
                for(i = 0; i < THREAD_NUM; i++){
                    tcBuf[i] = i + 1;
                    if( (pthread_create(&threadBuf[i], NULL,threadOpr1,
                        &tcBuf[i])) != 0 ) {
                        printf("pthread_create \n");
                        return -1;           
                    }    
                } 
                sleep(5);
                if( (pthread_create(&threadBuf[i], NULL,threadOpr2,
                    NULL)) != 0 ) {
                    printf("pthread_create \n");
                    return -1;           
                }
                sleep(30);
                printf("main exit\n");     
                return 0;  
            }
            /**********************************
            被殺死的線程函數
            ***********************************/
            void* threadOpr1(void* arg){
                int tcId=*(int *)arg;
                int i=0;
                while(i<10){
                   printf("%d live\n",tcId);
                   i++;
                   sleep(2); 
                }
                printf("killed exit\n");
               pthread_exit(NULL);
            }

            /**********************************
            殺死其他線程的線程
            ***********************************/
            void* threadOpr2(void* arg){
                int i=0;
                for(i = 0; i < THREAD_NUM; i++){
                    if( (pthread_kill(threadBuf[i],SIGTERM)) != 0 ) {
                        printf("pthread_kill \n");
                        pthread_exit(NULL);           
                    }    
                }
                sleep(30); 
                printf("killer exit \n");    
               pthread_exit(NULL);
            }
            結果:
            1 live
            2 live
            3 live
            1 live
            2 live
            3 live
            1 live
            2 live
            3 live
            Terminated


            久久人人爽人人爽人人片AV不| 狠狠色狠狠色综合久久 | 亚洲av伊人久久综合密臀性色| 亚洲午夜无码AV毛片久久| 久久人妻少妇嫩草AV无码蜜桃| 久久精品国产国产精品四凭| 办公室久久精品| 欧美激情精品久久久久久| 国产精品99久久免费观看| 狠狠色丁香婷婷久久综合| 久久福利资源国产精品999| 狠狠综合久久综合88亚洲| 久久久久久久综合综合狠狠| 一本色道久久99一综合| 精品一区二区久久| 伊人久久久AV老熟妇色| 久久久91精品国产一区二区三区| 欧洲国产伦久久久久久久| 亚洲人成精品久久久久| 久久综合视频网| 久久激情亚洲精品无码?V| 亚洲成色999久久网站| 久久久久综合网久久| 亚洲欧美成人久久综合中文网 | 久久激情亚洲精品无码?V| 人妻精品久久久久中文字幕一冢本| 久久电影网一区| 91久久婷婷国产综合精品青草 | 精品多毛少妇人妻AV免费久久| aaa级精品久久久国产片| 亚洲精品高清国产一久久| 久久久久久久尹人综合网亚洲 | 精品伊人久久大线蕉色首页| 久久香蕉国产线看观看乱码| 国产精品久久久久久久| 午夜人妻久久久久久久久| 91超碰碰碰碰久久久久久综合| 色婷婷综合久久久久中文| 久久精品国产精品亚洲下载| 久久青青草原亚洲av无码| 成人综合久久精品色婷婷 |