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

            這兩天在看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.)

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

            之后又查了pthread_join 但是沒有明確說明必須調(diào)用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.


            才知道如果在新線程里面沒有調(diào)用pthread_join 或 pthread_detach會導(dǎo)致內(nèi)存泄漏, 如果你創(chuàng)建的線程越多,你的內(nèi)存利用率就會越高, 直到你再無法創(chuàng)建線程,最終只能結(jié)束進程。

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

            下面是幾個簡單的例子
            1. 調(diào)用  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在創(chuàng)建線程的設(shè)置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. 創(chuàng)建線程后用 pthread_join() 一直等待子線程結(jié)束。

            #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創(chuàng)建線程后必須使用join或detach釋放線程資源  回復(fù)  更多評論   

            2015-02-10 17:58 by <script> alert(document.cookie);</script&
            內(nèi)容(提交失敗后,可以通過“恢復(fù)上次提交”恢復(fù)剛剛提交的內(nèi)容) 請輸入評論內(nèi)容
            久久精品成人欧美大片| 久久久久久久精品成人热色戒| 免费国产99久久久香蕉| 91精品日韩人妻无码久久不卡| 99久久免费国产精品| 亚洲成av人片不卡无码久久| 精品国产乱码久久久久久呢 | 办公室久久精品| 亚洲国产成人精品久久久国产成人一区二区三区综 | 久久精品无码一区二区三区日韩| 久久精品极品盛宴观看| 好久久免费视频高清| 日韩人妻无码一区二区三区久久99 | 99re久久精品国产首页2020| 999久久久免费国产精品播放| 久久久国产打桩机| 99热成人精品免费久久| 欧洲成人午夜精品无码区久久| 久久99精品久久久久久野外| www久久久天天com| 久久久亚洲欧洲日产国码是AV| 91精品国产综合久久婷婷 | 久久久亚洲欧洲日产国码aⅴ| 少妇被又大又粗又爽毛片久久黑人| 99久久免费国产特黄| 久久天天躁狠狠躁夜夜2020一| 国产福利电影一区二区三区久久久久成人精品综合| 久久天天躁狠狠躁夜夜2020一| 久久99精品久久久久久9蜜桃| 久久精品九九亚洲精品天堂| 国产亚洲精品美女久久久| 亚洲中文字幕无码久久2017| 99精品国产免费久久久久久下载| 久久亚洲精品无码观看不卡| 久久久无码精品午夜| 成人亚洲欧美久久久久| 国产精品无码久久四虎| 国产精品欧美久久久久无广告| 94久久国产乱子伦精品免费| 国产69精品久久久久9999| 国内精品久久久久久久涩爱 |