• <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&
            內容(提交失敗后,可以通過“恢復上次提交”恢復剛剛提交的內容) 請輸入評論內容
            国产精品久久久天天影视| 色诱久久av| 色综合久久88色综合天天 | 久久久久免费看成人影片| 久久久久亚洲精品无码蜜桃 | 久久久久久午夜成人影院| 7国产欧美日韩综合天堂中文久久久久| 国产成人无码精品久久久久免费| 香蕉久久永久视频| 精品国产福利久久久| 国内精品伊人久久久久妇| 久久精品9988| 久久青青草原精品国产| 久久伊人五月丁香狠狠色| 国产精品伊人久久伊人电影| 久久无码人妻一区二区三区| 久久久噜噜噜久久| 国产精品一区二区久久| 久久婷婷五月综合色高清| 久久午夜免费视频| 一本色道久久综合| 精品视频久久久久| 成人国内精品久久久久影院VR| 一本色道久久88精品综合| 亚洲国产日韩欧美综合久久| 国产精品欧美亚洲韩国日本久久| 久久久久久久亚洲Av无码| 久久久久亚洲av成人网人人软件 | 91精品久久久久久无码| 好属妞这里只有精品久久| 久久久久久国产精品免费无码| 中文字幕乱码久久午夜| 久久久亚洲裙底偷窥综合| 亚洲中文字幕无码久久2020| 亚洲综合久久久| 亚洲av日韩精品久久久久久a| 久久久久久国产a免费观看黄色大片 | 久久久老熟女一区二区三区| 久久综合给合久久狠狠狠97色| 日韩人妻无码精品久久久不卡 | 蜜桃麻豆www久久国产精品|