• <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&
            內容(提交失敗后,可以通過“恢復上次提交”恢復剛剛提交的內容) 請輸入評論內容
            久久综合久久鬼色| 精品国产婷婷久久久| 国产精品永久久久久久久久久| 国内精品久久久久影院一蜜桃| 一本色道久久99一综合| 国产精品视频久久| 2019久久久高清456| 久久久久一区二区三区| 久久99九九国产免费看小说| 日韩精品久久无码中文字幕| 欧美麻豆久久久久久中文| 久久99国产综合精品| 久久93精品国产91久久综合| 久久久久香蕉视频| 国产成人久久777777| 久久成人国产精品免费软件| 婷婷久久综合| 精品久久无码中文字幕| 欧美色综合久久久久久| 久久久久久久97| 精品无码久久久久久午夜| 久久夜色精品国产www| 国产精品久久久久jk制服| 久久久久人妻精品一区三寸蜜桃| 色欲久久久天天天综合网精品| 久久婷婷五月综合97色直播| 日本三级久久网| 久久久久99精品成人片试看| 狠狠综合久久综合88亚洲| 久久国产三级无码一区二区| 久久美女网站免费| 成人国内精品久久久久影院| 人妻精品久久无码区 | 精品久久人人妻人人做精品| 久久夜色精品国产欧美乱| 久久婷婷五月综合97色直播| 久久这里的只有是精品23| 漂亮人妻被中出中文字幕久久| 欧美一级久久久久久久大| 色偷偷88欧美精品久久久| 伊人久久成人成综合网222|