pthread_exit和pthread_join函数

文章讲述了在线程编程中,为什么不能直接使用`exit`函数,而应改用`pthread_exit`来终止单个线程,同时介绍了`pthread_exit`的用法、参数以及与`pthread_join`函数的关系,强调了内存管理的重要性。
摘要由CSDN通过智能技术生成

pthread_exit:

在线程中禁止调用exit函数,否则会导致整个进程退出,取而代之的是调用pthread_exit函数,这个函数只会使一个线程退出,如果主线程使用pthread_exit函数也不会使整个进程退出,不会影响其他线程的执行

函数原型:void pthread_exit(void *retval);

函数参数:retval通常传NULL

注意:pthread_exit或者return返回的指针所指向的内存单元必须是全局的或者使用nalloc分配的,不能在线程函数的栈上分配,因为当其他线程得到这个返回指针时,这个线程函数已经退出了,栈空间会被回收

通过以下代码我们可以发现子线程执行exit会让整个进程结束。

#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);//让子线程先执行
	printf("father thread,pid==[%d],id==[%ld]\n",getpid(),pthread_self());
}

 

可以发现主线程并没有执行

通过以下代码可以发现主线程执行pthread_exit函数后,子线程还可以执行:
 

#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);//保证主线程先执行
	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函数:

函数作用:阻塞等待线程退出,获取线程退出状态。其作用跟进程的waitpid()函数相似

函数原型:int pthread_join(pthread_t thread, void **retval);

函数返回值:

  • 成功返回0;
  • 失败返回错误号;

函数参数:

thread:线程id

retval:存储线程结束状态,整个指针和pthread_exit的参数是同一块内存地址 

#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);
}

可以发现p和pt的地址是一样的 ,pt存储了线程结束状态

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落落落sss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值