• <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>
            posts - 297,  comments - 15,  trackbacks - 0

            pthread_join函數及linux線程

            pthread_join使一個線程等待另一個線程結束。

             

            代碼中如果沒有pthread_join主線程會很快結束從而使整個進程結束,從而使創建的線程沒有機會開始執行就結束了。加入pthread_join后,主線程會一直等待直到等待的線程結束自己才結束,使創建的線程有機會執行。

             

            所有線程都有一個線程號,也就是Thread ID。其類型為pthread_t。通過調用pthread_self()函數可以獲得自身的線程號。

             

            下面說一下如何創建一個線程。

             

            通過創建線程,線程將會執行一個線程函數,該線程格式必須按照下面來聲明:

             

                   void * Thread_Function(void *)

             

            創建線程的函數如下:

             

                   int pthread_create(pthread_t *restrict thread,

             

                          const pthread_attr_t *restrict attr,

             

                          void *(*start_routine)(void*), void *restrict arg);

             

            下面說明一下各個參數的含義:

             

            thread:所創建的線程號。

             

            attr:所創建的線程屬性,這個將在后面詳細說明。

             

            start_routine:即將運行的線程函數。

             

            art:傳遞給線程函數的參數。

             

            下面是一個簡單的創建線程例子:

             

            #include <pthread.h>

             

            #include <stdio.h>

             

            /* Prints x’s to stderr. The parameter is unused. Does not return. */

             

            void* print_xs (void* unused)

             

            {

             

            while (1)

             

            fputc (‘x’, stderr);

             

            return NULL;

             

            }

             

            /* The main program. */

             

            int main ()

             

            {

             

            pthread_t thread_id;

             

            /* Create a new thread. The new thread will run the print_xs

             

            function. */

             

            pthread_create (&thread_id, NULL, &print_xs, NULL);

             

            /* Print o’s continuously to stderr. */

             

            while (1)

             

            fputc (‘o’, stderr);

             

            return 0;

             

            }

             

             

            在編譯的時候需要注意,由于線程創建函數在libpthread.so庫中,所以在編譯命令中需要將該庫導入。命令如下:

             

            gcc –o createthread –lpthread createthread.c

             

            如果想傳遞參數給線程函數,可以通過其參數arg,其類型是void *。如果你需要傳遞多個參數的話,可以考慮將這些參數組成一個結構體來傳遞。另外,由于類型是void *,所以你的參數不可以被提前釋放掉。

             

            下面一個問題和前面創建進程類似,不過帶來的問題回避進程要嚴重得多。如果你的主線程,也就是main函數執行的那個線程,在你其他縣城推出之前就已經退出,那么帶來的bug則不可估量。通過pthread_join函數會讓主線程阻塞,直到所有線程都已經退出。

             

            int pthread_join(pthread_t thread, void **value_ptr);

             

            thread:等待退出線程的線程號。

             

            value_ptr:退出線程的返回值。

             

            下面一個例子結合上面的內容:

             

            int main ()

             

            {

             

            pthread_t thread1_id;

             

            pthread_t thread2_id;

             

            struct char_print_parms thread1_args;

             

            struct char_print_parms thread2_args;

             

            /* Create a new thread to print 30,000 x’s. */

             

            thread1_args.character = ’x’;

             

            thread1_args.count = 30000;

             

            pthread_create (&thread1_id, NULL, &char_print, &thread1_args);

             

            /* Create a new thread to print 20,000 o’s. */

             

            thread2_args.character = ’o’;

             

            thread2_args.count = 20000;

             

            pthread_create (&thread2_id, NULL, &char_print, &thread2_args);

             

            /* Make sure the first thread has finished. */

             

            pthread_join (thread1_id, NULL);

             

            /* Make sure the second thread has finished. */

             

            pthread_join (thread2_id, NULL);

             

            /* Now we can safely return. */

             

            return 0;

             

            }

             

             

            下面說一下前面提到的線程屬性。

             

            在我們前面提到,可以通過pthread_join()函數來使主線程阻塞等待其他線程退 出,這樣主線程可以清理其他線程的環境。但是還有一些線程,更喜歡自己來清理退出的狀態,他們也不愿意主線程調用pthread_join來等待他們。我 們將這一類線程的屬性稱為detached。如果我們在調用pthread_create()函數的時候將屬性設置為NULL,則表明我們希望所創建的線 程采用默認的屬性,也就是jionable。如果需要將屬性設置為detached,則參考下面的例子:

             

            #include <stdio.h>

             

            #include <pthread.h>

             

            void * start_run(void * arg)

             

            {

             

                    //do some work

             

            }

             

            int main()

             

            {

             

                    pthread_t thread_id;

             

                    pthread_attr_t attr;

             

                    pthread_attr_init(&attr);

             

                    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

             

                    pthread_create(&thread_id,&attr,start_run,NULL);

             

                    pthread_attr_destroy(&attr);

             

                    sleep(5);

             

                    exit(0);

             

            }

             

             

            在線程設置為joinable后,可以調用pthread_detach()使之成為detached。但是相反的操作則不可以。還有,如果線程已經調用pthread_join()后,則再調用pthread_detach()則不會有任何效果。

             

            線程可以通過自身執行結束來結束,也可以通過調用pthread_exit()來結束線程的執行。另外,線程甲可以被線程乙被動結束。這個通過調用pthread_cancel()來達到目的。

             

            int pthread_cancel(pthread_t thread);

             

                   函數調用成功返回0

             

            當然,線程也不是被動的被別人結束。它可以通過設置自身的屬性來決定如何結束。

             

            線程的被動結束分為兩種,一種是異步終結,另外一種是同步終結。異步終結就是當其他線程調用 pthread_cancel的時候,線程就立刻被結束。而同步終結則不會立刻終結,它會繼續運行,直到到達下一個結束點(cancellation point)。當一個線程被按照默認的創建方式創建,那么它的屬性是同步終結。

             

            通過調用pthread_setcanceltype()來設置終結狀態。

             

            int pthread_setcanceltype(int type, int *oldtype);

             

            state:要設置的狀態,可以為PTHREAD_CANCEL_DEFERRED或者為PTHREAD_CANCEL_ASYNCHRONOUS

             

            那么前面提到的結束點又是如何設置了?最常用的創建終結點就是調用pthread_testcancel()的地方。該函數除了檢查同步終結時的狀態,其他什么也不做。

             

            上面一個函數是用來設置終結狀態的。還可以通過下面的函數來設置終結類型,即該線程可不可以被終結:

             

            int pthread_setcancelstate(int state, int *oldstate);

             

                   state:終結狀態,可以為PTHREAD_CANCEL_DISABLE或者PTHREAD_CANCEL_ENABLE。具體什么含義大家可以通過單詞意思即可明白。

             

            最后說一下線程的本質。其實在Linux中,新建的線程并不是在原先的進程中,而是系統通過 一個系統調用clone()。該系統copy了一個和原先進程完全一樣的進程,并在這個進程中執行線程函數。不過這個copy過程和fork不一樣。 copy后的進程和原先的進程共享了所有的變量,運行環境。這樣,原先進程中的變量變動在copy后的進程中便能體現出來。


            from:

            http://blog.csdn.net/jxxfqyy/archive/2009/04/16/4084193.aspx

            posted on 2010-08-24 18:01 chatler 閱讀(2634) 評論(0)  編輯 收藏 引用 所屬分類: Linux_Coding
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(10)

            隨筆分類(307)

            隨筆檔案(297)

            algorithm

            Books_Free_Online

            C++

            database

            Linux

            Linux shell

            linux socket

            misce

            • cloudward
            • 感覺這個博客還是不錯,雖然做的東西和我不大相關,覺得看看還是有好處的

            network

            OSS

            • Google Android
            • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
            • os161 file list

            overall

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            国产一区二区精品久久凹凸| 无码人妻久久一区二区三区蜜桃| 日韩久久久久中文字幕人妻| 久久r热这里有精品视频| 亚洲精品乱码久久久久久蜜桃图片| 日日狠狠久久偷偷色综合免费| 欧美伊香蕉久久综合类网站| 91精品国产乱码久久久久久| 一本久道久久综合狠狠爱| 99久久夜色精品国产网站| 亚洲va久久久久| 欧美一区二区久久精品| 免费精品国产日韩热久久| 综合久久一区二区三区| 久久精品免费全国观看国产| 波多野结衣久久一区二区| 久久人妻AV中文字幕| 狠狠色噜噜色狠狠狠综合久久| 亚洲精品无码专区久久久| 久久久噜噜噜www成人网| A狠狠久久蜜臀婷色中文网| 秋霞久久国产精品电影院| 久久线看观看精品香蕉国产| 一级做a爰片久久毛片16| 久久91精品综合国产首页| 狠狠色丁香久久婷婷综合图片| 久久亚洲sm情趣捆绑调教| 人妻少妇久久中文字幕一区二区| 久久精品国产精品亚洲毛片| 老司机国内精品久久久久| 久久亚洲欧洲国产综合| 精品国产乱码久久久久久呢| 久久久久久亚洲AV无码专区| 亚洲欧美精品伊人久久| 成人综合久久精品色婷婷| 99久久精品毛片免费播放| 久久久人妻精品无码一区| 久久久久久久久波多野高潮| 欧美精品一区二区精品久久| 亚洲精品乱码久久久久久蜜桃 | 伊人久久大香线蕉无码麻豆|