pthread_join()函數(shù)原型:
int pthread_join(pthread_t thread, void **retval);
args:
pthread_t thread: 被連接線程的線程號
void **retval : 指向一個指向被連接線程的返回碼的指針的指針
return:
線程連接的狀態(tài),0是成功,非0是失敗
1.為什么要使用pthread_join()
在很多情況下,主線程生成并起動了子線程,如果子線程里要進行大量的耗時的運算,主線程往往將于子線程之前結(jié)束,但是如果主線程處理完其他的事務(wù)后,需要用到子線程的處理結(jié)果,也就是主線程需要等待子線程執(zhí)行完成之后再結(jié)束,這個時候就要用到pthread_join()方法了。
即pthread_join()的作用可以這樣理解:主線程等待子線程的終止。也就是在子線程調(diào)用了pthread_join()方法后面的代碼,只有等到子線程結(jié)束了才能執(zhí)行。
當A線程調(diào)用線程B并 pthread_join() 時,A線程會處于阻塞狀態(tài),直到B線程結(jié)束后,A線程才會繼續(xù)執(zhí)行下去。當 pthread_join() 函數(shù)返回后,被調(diào)用線程才算真正意義上的結(jié)束,它的內(nèi)存空間也會被釋放(如果被調(diào)用線程是非分離的)。這里有三點需要注意:
被釋放的內(nèi)存空間僅僅是系統(tǒng)空間,你必須手動清除程序分配的空間,比如 malloc() 分配的空間。
2.一個線程只能被一個線程所連接。
3.被連接的線程必須是非分離的,否則連接會出錯。
所以可以看出pthread_join()有兩種作用:用于等待其他線程結(jié)束:當調(diào)用 pthread_join() 時,當前線程會處于阻塞狀態(tài),直到被調(diào)用的線程結(jié)束后,當前線程才會重新開始執(zhí)行。
對線程的資源進行回收:如果一個線程是非分離的(默認情況下創(chuàng)建的線程都是非分離)并且沒有對該線程使用 pthread_join() 的話,該線程結(jié)束后并不會釋放其內(nèi)存空間,這會導(dǎo)致該線程變成了“僵尸線程”。
2.使用方式
pthread_t tid;
pthread_create(&tid, NULL, thread_run,NULL);
pthread_join(tid,NULL);
創(chuàng)建線程之后直接調(diào)用pthread_join方法就行了。
3.代碼實驗
可以通過代碼來看看執(zhí)行的效果,就知道了:
#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這個線程執(zhí)行完畢自己才結(jié)束
// 一般項目中需要子線程計算后的值就需要加join方法
pthread_join(tid,NULL);
// 如果沒有join方法可以看看打印的順序
printf("The count is = %d\n",count);
getchar();
return 0;
}
加了pthread_join()方法的打?。?/p>
如果把里面的pthread_join()方法注釋掉的打?。?/p>
可以看得出來,如果沒有加pthread_join()方法,main線程里面直接就執(zhí)行起走了,加了之后是等待線程執(zhí)行了之后才執(zhí)行的后面的代碼。