<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ### 一、使用方式 > pthread_t tid; > pthread_create(&tid, NULL, thread_run,NULL); > pthread_join(tid,NULL); 創建線程之后直接調用pthread_join方法就行了。 ### 二、為什么要使用pthread_join() ? ? ?在很多情況下,主線程生成并起動了子線程,如果子線程里要進行大量的耗時的運算,主線程往往將于子線程之前結束,但是如果主線程處理完其他的事務后,需要用到子線程的處理結果,也就是主線程需要等待子線程執行完成之后再結束,這個時候就要用到pthread_join()方法了。 ? ? ? 即pthread_join()的作用可以這樣理解:主線程等待子線程的終止。也就是在子線程調用了pthread_join()方法后面的代碼,只有等到子線程結束了才能執行。 ### 三、代碼實驗 可以通過代碼來看看執行的效果,就知道了: ~~~ #include "stdafx.h" #include <pthread.h> #include <stdio.h> #include <Windows.h> #pragma comment(lib, "pthreadVC2.lib") static int count = 0; void* thread_run(void* parm) { for (int i=0;i<5;i++) { count++; printf("The thread_run method count is = %d\n",count); Sleep(1000); } return NULL; } int main() { pthread_t tid; pthread_create(&tid, NULL, thread_run,NULL); // 加入pthread_join后,主線程"main"會一直等待直到tid這個線程執行完畢自己才結束 // 一般項目中需要子線程計算后的值就需要加join方法 pthread_join(tid,NULL); // 如果沒有join方法可以看看打印的順序 printf("The count is = %d\n",count); getchar(); return 0; } ~~~ 加了pthread_join()方法的打印: ![](https://box.kancloud.cn/2016-08-19_57b6ce7d90e3c.jpg) 如果把里面的pthread_join()方法注釋掉的打印: ![](https://box.kancloud.cn/2016-08-19_57b6ce7daf658.jpg) 可以看得出來,如果沒有加pthread_join()方法,main線程里面直接就執行起走了,加了之后是等待線程執行了之后才執行的后面的代碼。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看