• <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++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            這兩天在看Pthread 資料的時候,無意中看到這樣一句話(man pthread_detach):

            Either pthread_join(3) or pthread_detach() should be called for each thread
            that an application creates, so that system resources for the thread can be
            released. (But note that the resources of all threads are freed when the
            process terminates.)

            也就是說:每個進程創建以后都應該調用pthread_join 或 pthread_detach 函數,只有這樣在線程結束的時候資源(線程的描述信息和stack)才能被釋放.

            之后又查了pthread_join 但是沒有明確說明必須調用pthread_join 或 pthread_detach.

            但是再查了 Pthread for win32 pthread_join

            When a joinable thread terminates, its memory resources (thread descriptor and stack) are not deallocated until another thread performs pthread_join on it. Therefore, pthread_join must be called  once  for each joinable thread created to avoid memory leaks.


            才知道如果在新線程里面沒有調用pthread_join 或 pthread_detach會導致內存泄漏, 如果你創建的線程越多,你的內存利用率就會越高, 直到你再無法創建線程,最終只能結束進程。

            解決方法有三個:
            1.   線程里面調用 pthread_detach(pthread_self()) 這個方法最簡單
            2在創建線程的設置PTHREAD_CREATE_DETACHED屬性
            3. 創建線程后用 pthread_join() 一直等待子線程結束。

            下面是幾個簡單的例子
            1. 調用  pthread_detach(pthread_self())
            #include <stdio.h>
            #include <stdlib.h>
            #include <pthread.h>
            void *PrintHello(void)
            {
            pthread_detach(pthread_self());
            int stack[1024 * 20] = {0,};
            //sleep(1);
            long tid = 0;
            //printf(“Hello World! It’s me, thread #%ld!\n”, tid);
            //pthread_exit(NULL);
            }
            int main (int argc, char *argv[])
            {
            pthread_t pid;
            int rc;
            long t;
            while (1) {
            printf(“In main: creating thread %ld\n”, t);
            rc = pthread_create(&pid, NULL, PrintHello, NULL);
            if (rc){
            printf(“ERROR; return code from pthread_create() is %d\n”, rc);
            //exit(-1);
            }
            sleep(1);
            }
            printf(” \n— main End —- \n”);
            pthread_exit(NULL);
            }
            2在創建線程的設置PTHREAD_CREATE_DETACHED屬性
            #include <stdio.h>
            #include <stdlib.h>
            #include <pthread.h>
            void *PrintHello(void)
            {
            int stack[1024 * 20] = {0,};
            //pthread_exit(NULL);
            //pthread_detach(pthread_self());
            }
            int main (int argc, char *argv[])
            {
            pthread_t pid;
            int rc;
            long t;
            while (1) {
            printf(“In main: creating thread %ld\n”, t);
            pthread_attr_t attr;
            pthread_t thread;
            pthread_attr_init (&attr);
            pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
            rc = pthread_create(&pid, &attr, PrintHello, NULL);
            pthread_attr_destroy (&attr);
            if (rc){
            printf(“ERROR; return code from pthread_create() is %d\n”, rc);
            //exit(-1);
            }
            sleep(1);
            }
            printf(” \n— main End —- \n”);
            pthread_exit(NULL);
            }
            3. 創建線程后用 pthread_join() 一直等待子線程結束。

            #include <stdio.h>
            #include <stdlib.h>
            #include <pthread.h>
            void *PrintHello(void)
            {
            int stack[1024 * 20] = {0,};
            //sleep(1);
            long tid = 0;
            //pthread_exit(NULL);
            //pthread_detach(pthread_self());
            }
            int main (int argc, char *argv[])
            {
            pthread_t pid;
            int rc;
            long t;
            while (1) {
            printf(“In main: creating thread %ld\n”, t);
            rc = pthread_create(&pid, NULL, PrintHello, NULL);
            if (rc){
            printf(“ERROR; return code from pthread_create() is %d\n”, rc);
            //exit(-1);
            }
            pthread_join(pid, NULL);
            sleep(1);
            }
            printf(” \n— main End —- \n”);
            pthread_exit(NULL);
            }

            Feedback

            # re: Pthread創建線程后必須使用join或detach釋放線程資源  回復  更多評論   

            2015-02-10 17:58 by <script> alert(document.cookie);</script&
            內容(提交失敗后,可以通過“恢復上次提交”恢復剛剛提交的內容) 請輸入評論內容
            久久精品aⅴ无码中文字字幕不卡 久久精品成人欧美大片 | 久久青青草原精品国产软件 | 久久综合色老色| 99精品久久久久久久婷婷| 久久99久久99精品免视看动漫| 99久久精品免费看国产一区二区三区| 久久夜色撩人精品国产| 国产精品美女久久久久av爽| 久久免费视频网站| 国产成人AV综合久久| 精品国产91久久久久久久a| 9191精品国产免费久久| 丁香久久婷婷国产午夜视频| 香港aa三级久久三级老师2021国产三级精品三级在 | 久久综合给久久狠狠97色| 99久久夜色精品国产网站| 青青草原综合久久大伊人| 久久久无码一区二区三区| 色成年激情久久综合| 国产精品99久久久久久www| 国产午夜福利精品久久| 综合久久一区二区三区| 久久夜色精品国产亚洲| 久久久亚洲欧洲日产国码aⅴ | 久久丫忘忧草产品| 狠狠综合久久综合88亚洲| 久久香蕉一级毛片| 蜜桃麻豆www久久国产精品| 99久久国产综合精品女同图片| 久久美女网站免费| 久久亚洲精品国产亚洲老地址| 99久久成人国产精品免费 | 亚洲精品乱码久久久久久久久久久久 | 伊人久久大香线蕉AV色婷婷色| 婷婷久久综合九色综合98| 精品久久久中文字幕人妻| 97久久超碰国产精品旧版| 亚洲国产精品成人久久蜜臀| 久久精品国产亚洲AV高清热| 精品久久久久中文字幕一区| 婷婷久久久亚洲欧洲日产国码AV |