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


            国产精品成人精品久久久| 国产V亚洲V天堂无码久久久| 久久精品国产一区二区三区| 久久天天日天天操综合伊人av| 久久久久久极精品久久久| 亚洲国产另类久久久精品| 91精品国产91久久| 久久久国产视频| 久久国产精品99久久久久久老狼| 亚洲国产天堂久久久久久| 久久国产欧美日韩精品| 亚洲乱码日产精品a级毛片久久| 无码人妻精品一区二区三区久久| 91久久精品无码一区二区毛片| 久久久精品国产免大香伊| 久久久久亚洲av毛片大| 久久99国产综合精品免费| 久久久久亚洲精品日久生情| 久久久久久a亚洲欧洲aⅴ| 久久精品www| 午夜精品久久久久久久久| 久久婷婷色综合一区二区| 亚洲国产精品人久久| 精品久久久久久中文字幕人妻最新| 欧美久久久久久精选9999| 国内精品久久久久久久亚洲 | 97久久超碰国产精品旧版| 模特私拍国产精品久久| 久久久精品久久久久久| 伊人久久免费视频| 美女写真久久影院| 国产精品美女久久久| 99久久久精品免费观看国产| 午夜天堂精品久久久久| 久久精品国产99久久无毒不卡| 亚洲AV无码1区2区久久| 亚洲av日韩精品久久久久久a| 久久精品卫校国产小美女| 久久久久se色偷偷亚洲精品av| 亚洲国产另类久久久精品黑人| 久久人人爽人人爽人人片AV不|