C語言中pthread_exit和pehread_join的使用
pthread_exit:
在線程中禁止調(diào)用exit函數(shù),否則會導(dǎo)致整個進(jìn)程退出,取而代之的是調(diào)用pthread_exit函數(shù),這個函數(shù)只會使一個線程退出,如果主線程使用pthread_exit函數(shù)也不會使整個進(jìn)程退出,不會影響其他線程的執(zhí)行
函數(shù)原型:void pthread_exit(void *retval);
函數(shù)參數(shù):retval通常傳NULL
注意:pthread_exit或者return返回的指針?biāo)赶虻膬?nèi)存單元必須是全局的或者使用nalloc分配的,不能在線程函數(shù)的棧上分配,因為當(dāng)其他線程得到這個返回指針時,這個線程函數(shù)已經(jīng)退出了,??臻g會被回收
通過以下代碼我們可以發(fā)現(xiàn)子線程執(zhí)行exit會讓整個進(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;
- 失敗返回錯誤號;
函數(shù)參數(shù):
thread:線程id
retval:存儲線程結(jié)束狀態(tài),整個指針和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存儲了線程結(jié)束狀態(tài)
到此這篇關(guān)于java中pthread_exit和pehread_join函數(shù)的使用的文章就介紹到這了,更多相關(guān)java pthread_exit pehread_join函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++中實現(xiàn)WebSocket通信的兩種方法:libwebsockets庫、Boost.Beast?庫
C++中WebSocket庫主要有以下幾個?:cpp-websocket?、asio_websocket?、websockets++?、?websocketpp?、?libwebsockets?、?uWebSockets?、Boost.Beast?、Simple-WebSocket-Server?,這篇文章使用libwebsockets庫、Boost.Beast?庫來實現(xiàn)c++中的WebSocket通信2025-01-01C語言函數(shù)指針與回調(diào)函數(shù)的實現(xiàn)
本文主要介紹了C語言函數(shù)指針與回調(diào)函數(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05vs2022重新編譯opencv-python?cuda加速時報錯的問題解決
本文主要介紹了vs2022重新編譯opencv-python?cuda加速時報錯,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Objective-C限制函數(shù)調(diào)用的頻率詳解
這篇文章主要給大家介紹了關(guān)于Objective-C限制函數(shù)調(diào)用的頻率的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12