• <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&
            內容(提交失敗后,可以通過“恢復上次提交”恢復剛剛提交的內容) 請輸入評論內容
            国产午夜福利精品久久2021 | 久久久久免费精品国产| 久久国产AVJUST麻豆| 精品999久久久久久中文字幕| 久久97久久97精品免视看秋霞| 亚洲Av无码国产情品久久| 久久99精品国产99久久| 亚洲国产成人久久笫一页| 久久精品夜色噜噜亚洲A∨| 精品综合久久久久久97超人| 久久精品国产精品亚洲| 久久99精品久久久久久hb无码| 2020最新久久久视精品爱| 国产精品久久波多野结衣| 久久久久亚洲AV无码麻豆| 伊人久久精品影院| 激情伊人五月天久久综合| 国产精品成人久久久| 久久国产美女免费观看精品| 99精品久久精品一区二区| 国产亚洲精品自在久久| 蜜臀av性久久久久蜜臀aⅴ | 99精品久久精品一区二区| 免费无码国产欧美久久18| 久久99久久无码毛片一区二区| 99国产欧美久久久精品蜜芽| 亚洲中文字幕无码久久精品1| 久久久一本精品99久久精品88| 久久久久亚洲AV成人网| 亚洲综合久久综合激情久久| 久久亚洲精品中文字幕| 久久精品国产亚洲AV嫖农村妇女| 无码人妻精品一区二区三区久久久| 日韩人妻无码一区二区三区久久| 思思久久99热只有频精品66| 精品永久久福利一区二区| 国产成人久久AV免费| 国内精品伊人久久久久网站| 久久久久综合国产欧美一区二区| 久久久久99精品成人片牛牛影视| 亚洲午夜精品久久久久久浪潮 |