最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言中pthread_exit和pehread_join的使用

 更新時(shí)間:2024年02月27日 09:17:53   作者:落落落sss  
pthread_exit用于強(qiáng)制退出一個(gè)線程,pthread_join用于阻塞等待線程退出,獲取線程退出狀態(tài),本文主要介紹了C語言中pthread_exit和pehread_join函數(shù)的使用,具有一定的參考價(jià)值,感興趣的可以了解一下

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í)例

    這篇文章主要介紹了C++ 實(shí)現(xiàn)優(yōu)先隊(duì)列的簡單實(shí)例的相關(guān)資料,希望通過本文能幫助大家實(shí)現(xiàn)優(yōu)先隊(duì)列,需要的朋友可以參考下
    2017-08-08
  • C/C++中派生類訪問屬性詳解及其作用介紹

    C/C++中派生類訪問屬性詳解及其作用介紹

    這篇文章主要介紹了C/C++中派生類訪問屬性詳解及其作用介紹,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • C++之動(dòng)態(tài)數(shù)組vector解讀

    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)線性表教程示例詳解

    這篇文章主要為大家介紹了C語言數(shù)據(jù)結(jié)構(gòu)線性表的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-02-02
  • C++中std::vector的具體使用

    C++中std::vector的具體使用

    C++標(biāo)準(zhǔn)庫中的std::vector是一種動(dòng)態(tài)數(shù)組容器,適用于算法競賽中的動(dòng)態(tài)數(shù)據(jù)存儲(chǔ)、數(shù)組擴(kuò)展和模擬棧/二維數(shù)組等場景,本文就來介紹一下,感興趣的可以了解一下
    2025-02-02
  • VS2022+libtorch+Cuda11.3安裝測試教程詳解(調(diào)用cuda)

    VS2022+libtorch+Cuda11.3安裝測試教程詳解(調(diào)用cuda)

    這篇文章主要介紹了VS2022+libtorch+Cuda11.3安裝測試(調(diào)用cuda),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • 如何用C++求兩個(gè)數(shù)的最大公約數(shù)和最小公倍數(shù)

    如何用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
  • C++左值與右值核心判斷方法

    C++左值與右值核心判斷方法

    C++ 11 引入右值引用和移動(dòng)語義后,左值右值的區(qū)分變得更加重要,本文介紹C++左值與右值核心判斷方法,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2026-06-06
  • C語言詳細(xì)講解二分查找用法

    C語言詳細(xì)講解二分查找用法

    二分查找法,又叫做折半查找法,它是一種效率較高的查找方法。但是,折半查找要求線性表必須采用順序存儲(chǔ)結(jié)構(gòu),而且表中元素按關(guān)鍵字有序排列
    2022-04-04
  • Cocos2d-x UI開發(fā)之CCControlPotentiometer控件類使用實(shí)例

    Cocos2d-x UI開發(fā)之CCControlPotentiometer控件類使用實(shí)例

    這篇文章主要介紹了Cocos2d-x UI開發(fā)之CCControlPotentiometer控件類使用實(shí)例,本文代碼中包含注釋來講解CCControlPotentiometer控件類的使用,需要的朋友可以參考下
    2014-09-09

最新評(píng)論

景谷| 哈巴河县| 霍山县| 神池县| 浦北县| 怀柔区| 金门县| 周口市| 长顺县| 元朗区| 光泽县| 云梦县| 巴南区| 东兴市| 云霄县| 黔西| 呈贡县| 昌乐县| 内乡县| 文登市| 阿拉善盟| 明星| 靖远县| 富宁县| 师宗县| 翁牛特旗| 红河县| 奉化市| 梅河口市| 徐水县| 定安县| 土默特左旗| 章丘市| 拜泉县| 石阡县| 肃宁县| 汕尾市| 高雄县| 哈尔滨市| 雅安市| 石楼县|