• <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 閱讀(434) 評論(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


            色综合久久中文色婷婷| 久久精品免费一区二区| 91久久精一区二区三区大全| 青青青国产成人久久111网站| 亚洲国产精品久久久久| 国内精品伊人久久久久妇| 久久久久久久女国产乱让韩| 久久91精品国产91久久户| 国产一区二区久久久| 久久国产欧美日韩精品| 久久精品成人免费国产片小草| 亚洲精品乱码久久久久久久久久久久| 久久久久久国产精品无码超碰| 久久99精品国产麻豆不卡| 久久人妻无码中文字幕| 91精品日韩人妻无码久久不卡| 亚洲香蕉网久久综合影视 | 色综合合久久天天综合绕视看| 亚洲а∨天堂久久精品9966| AV狠狠色丁香婷婷综合久久| 少妇久久久久久被弄到高潮| 国产精品久久国产精品99盘| 囯产精品久久久久久久久蜜桃| 久久精品国产WWW456C0M| 久久久久久综合一区中文字幕| 亚洲国产精品18久久久久久| 亚洲国产精品无码久久九九| 国产精品日韩深夜福利久久| 狠狠色丁香久久综合五月| 国产精品久久久久jk制服| 人妻精品久久久久中文字幕69| 99久久精品免费看国产一区二区三区 | 国产精品久久久久影院色| 午夜精品久久久久久久久| 久久久久国产精品人妻| 久久久久久国产精品无码下载| 欧美伊人久久大香线蕉综合| 中文字幕久久亚洲一区| 国产毛片欧美毛片久久久| 亚洲精品无码专区久久久| 日产精品99久久久久久|