C語言中pthread_exit和pehread_join的使用
pthread_exit:
在線程中禁止調(diào)用exit函數(shù),否則會(huì)導(dǎo)致整個(gè)進(jìn)程退出,取而代之的是調(diào)用pthread_exit函數(shù),這個(gè)函數(shù)只會(huì)使一個(gè)線程退出,如果主線程使用pthread_exit函數(shù)也不會(huì)使整個(gè)進(jìn)程退出,不會(huì)影響其他線程的執(zhí)行
函數(shù)原型:void pthread_exit(void *retval);
函數(shù)參數(shù):retval通常傳NULL
注意:pthread_exit或者return返回的指針?biāo)赶虻膬?nèi)存單元必須是全局的或者使用nalloc分配的,不能在線程函數(shù)的棧上分配,因?yàn)楫?dāng)其他線程得到這個(gè)返回指針時(shí),這個(gè)線程函數(shù)已經(jīng)退出了,??臻g會(huì)被回收
通過以下代碼我們可以發(fā)現(xiàn)子線程執(zhí)行exit會(huì)讓整個(gè)進(jìn)程結(jié)束。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include <pthread.h>
void *mythread(void *arg)
{
printf("child thread,pid==[%d],id==[%ld]\n",getpid(),pthread_self());
exit(0);
}
int main()
{
//int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
// void *(*start_routine) (void *), void *arg);
pthread_t thread;
int ret=pthread_create(&thread,NULL,mythread,NULL);
if(ret!=0)
{
printf("pthread_create error:[%s]\n",strerror(ret));
return -1;
}
sleep(1);//讓子線程先執(zhí)行
printf("father thread,pid==[%d],id==[%ld]\n",getpid(),pthread_self());
}
可以發(fā)現(xiàn)主線程并沒有執(zhí)行
通過以下代碼可以發(fā)現(xiàn)主線程執(zhí)行pthread_exit函數(shù)后,子線程還可以執(zhí)行:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include <pthread.h>
void *mythread(void *arg)
{
sleep(1);//保證主線程先執(zhí)行
printf("child thread,pid==[%d],id==[%ld]\n",getpid(),pthread_self());
}
int main()
{
//int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
// void *(*start_routine) (void *), void *arg);
pthread_t thread;
int ret=pthread_create(&thread,NULL,mythread,NULL);
if(ret!=0)
{
printf("pthread_create error:[%s]\n",strerror(ret));
return -1;
}
printf("father thread,pid==[%d],id==[%ld]\n",getpid(),pthread_self());
pthread_exit(NULL);
}

pthread_join函數(shù):
函數(shù)作用:阻塞等待線程退出,獲取線程退出狀態(tài)。其作用跟進(jìn)程的waitpid()函數(shù)相似
函數(shù)原型:int pthread_join(pthread_t thread, void **retval);
函數(shù)返回值:
- 成功返回0;
- 失敗返回錯(cuò)誤號(hào);
函數(shù)參數(shù):
thread:線程id
retval:存儲(chǔ)線程結(jié)束狀態(tài),整個(gè)指針和pthread_exit的參數(shù)是同一塊內(nèi)存地址
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include <pthread.h>
void *mythread(void *arg)
{
int *p=(int *)malloc(sizeof(int));(或者用全局變量)
*p=9;
printf("child thread,id==[%ld],add==[%p]\n",pthread_self(),p);
pthread_exit(p);
}
int main()
{
//int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
// void *(*start_routine) (void *), void *arg);
pthread_t thread;
int ret=pthread_create(&thread,NULL,mythread,NULL);
if(ret!=0)
{
printf("pthread_create error:[%s]\n",strerror(ret));
return -1;
}
// int pthread_join(pthread_t thread, void **retval);
void *pt=malloc(sizeof(void));
pthread_join(thread,&pt);
int n=*(int *)pt;
printf("child exit status:[%d],add==[%p]\n",n,pt);
}
可以發(fā)現(xiàn)p和pt的地址是一樣的 ,pt存儲(chǔ)了線程結(jié)束狀態(tài)
到此這篇關(guān)于java中pthread_exit和pehread_join函數(shù)的使用的文章就介紹到這了,更多相關(guān)java pthread_exit pehread_join函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++ 實(shí)現(xiàn)優(yōu)先隊(duì)列的簡單實(shí)例
這篇文章主要介紹了C++ 實(shí)現(xiàn)優(yōu)先隊(duì)列的簡單實(shí)例的相關(guān)資料,希望通過本文能幫助大家實(shí)現(xiàn)優(yōu)先隊(duì)列,需要的朋友可以參考下2017-08-08
C++之動(dòng)態(tài)數(shù)組vector解讀
這篇文章主要介紹了C++之動(dòng)態(tài)數(shù)組vector解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-06-06
C語言數(shù)據(jù)結(jié)構(gòu)線性表教程示例詳解
這篇文章主要為大家介紹了C語言數(shù)據(jù)結(jié)構(gòu)線性表的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-02-02
VS2022+libtorch+Cuda11.3安裝測(cè)試教程詳解(調(diào)用cuda)
這篇文章主要介紹了VS2022+libtorch+Cuda11.3安裝測(cè)試(調(diào)用cuda),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
如何用C++求兩個(gè)數(shù)的最大公約數(shù)和最小公倍數(shù)
最大公約數(shù)是指兩個(gè)或多個(gè)整數(shù)共有約數(shù)中,最大的一個(gè)約數(shù),常用的方法是歐幾里得算法,也叫輾轉(zhuǎn)相除法,下面這篇文章主要給大家介紹了關(guān)于如何用C++求兩個(gè)數(shù)的最大公約數(shù)和最小公倍數(shù)的相關(guān)資料,需要的朋友可以參考下2023-01-01
Cocos2d-x UI開發(fā)之CCControlPotentiometer控件類使用實(shí)例
這篇文章主要介紹了Cocos2d-x UI開發(fā)之CCControlPotentiometer控件類使用實(shí)例,本文代碼中包含注釋來講解CCControlPotentiometer控件類的使用,需要的朋友可以參考下2014-09-09

