• <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&
            內容(提交失敗后,可以通過“恢復上次提交”恢復剛剛提交的內容) 請輸入評論內容
            日韩亚洲欧美久久久www综合网| 无码人妻久久一区二区三区免费| 久久精品国产91久久麻豆自制| 久久福利青草精品资源站| 88久久精品无码一区二区毛片 | 久久99精品久久久久久久不卡| 久久se精品一区二区| 欧美日韩精品久久久免费观看| 国产精品久久久久久久久软件| 久久亚洲精品成人av无码网站| 国产999精品久久久久久| 久久精品人妻中文系列| 亚洲国产精品久久久久婷婷老年| 久久99九九国产免费看小说| 久久福利青草精品资源站免费| 久久久久久国产精品无码下载| 国产精品99久久久久久www| 久久99热这里只有精品国产| 久久99精品国产麻豆蜜芽| 热re99久久6国产精品免费| 热RE99久久精品国产66热| 99久久精品国内| 婷婷五月深深久久精品| 久久久噜噜噜久久中文字幕色伊伊| 久久天堂电影网| 国产精品久久久久久| 亚洲人成伊人成综合网久久久| 久久精品国产99久久香蕉| 久久精品国产亚洲一区二区| 久久精品无码一区二区无码 | A级毛片无码久久精品免费| 久久夜色精品国产噜噜亚洲AV| 久久久国产99久久国产一| 久久综合一区二区无码| 久久久久久av无码免费看大片| 91精品国产高清久久久久久国产嫩草| 久久综合给合久久狠狠狠97色 | 精品久久久久久国产牛牛app| 久久精品国产久精国产| 亚洲国产成人久久精品动漫| 99久久亚洲综合精品成人|