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

  • 函数描述:将单个线程退出
  • 函数原型:void pthread_exit(void *retval);
  • 函数参数:retval表示线程退出状态,通常传NULL

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

如果在主线程中使用pthread_exit函数,会导致子线程还在,内存无法被回收,成为僵尸进程。引入pthread_join函数。

pthread_join函数

  • 函数描述:阻塞等待线程退出,获取线程退出状态。其作用,对应进程中的waitpid() 函数。
  • 函数原型:int pthread_join(pthread_t thread, void **retval);
  • 函数返回值:成功:0;失败:错误号
  • 函数参数:
  1. thread:线程ID
  2. retval:存储线程结束状态,整个指针和pthread_exit的参数是同一块内存地址。

引入一个例子使主线程获取子线程的退出状态:

代码如下:

#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>struct Test
{int data;char name[64];
};
int g_var = 9;
struct Test t;//线程执行函数
void *mythread(void *arg)
{printf("child thread, pid==[%d], id==[%ld]\n", getpid(), pthread_self());//printf("[%p]\n", &g_var);//void pthread_exit(void *retval);//pthread_exit(&g_var);memset(&t, 0x00, sizeof(t));t.data = 99;strcpy(t.name, "xiaowen");pthread_exit(&t);
}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("main thread, pid==[%d], id==[%ld]\n", getpid(), pthread_self());//回收子线程void *p = NULL;//       int pthread_join(pthread_t thread, void **retval);pthread_join(thread, &p);       //&p中存放&g_var的地址//int n = *(int *)p;struct Test *pt = (struct Test *)p;printf("child exit status:[%d],[%s],[%p]\n",  pt->data, pt->name, p);return 0;
}
~      

结果如下:主线程获取了子线程的退出状态:即结构体中的内容。

pthread_exit函数相关推荐

  1. 【Linux多线程】pthread_exit函数

    线程的终止可以调用pthread_exit函数来结束. 也就是说,一个线程可以隐式的退出,也可以显式的调用pthread_exit函数来退出. pthread_exit函数唯一的参数value_ptr ...

  2. linux线程函数大全

    以下内容来源于网络,本人仅收集用于学习之用,如原作者有任何意见,可以给我留言,我会尽快将其删除. 索引: 1.创建线程pthread_create 2.等待线程结束pthread_join 3.分离线 ...

  3. 【Linux 线程】常用线程函数复习《一》

    1.pthread_create以及pthread_self函数 1 /**************************************************************** ...

  4. 《APUE》中的函数整理

    第1章 unix基础知识 1. char *strerror(int errnum) 该函数将errnum(就是errno值)映射为一个出错信息字符串,返回该字符串指针.声明在string.h文件中. ...

  5. UNIX再学习 -- exit 和 wait 系列函数

    我们一开始讲进程环境时,就有提到了.进程有 8 种方式使进程终止. 其中 5 种为正常终止,它们是: (1)在 main 函数中执行 return (2)调用 exit 函数,并不处理文件描述符,多进 ...

  6. linux中的线程函数

    函数pthread_create  作用:创建线程  函数原型:int pthread_create(pthread_t * tidp,const pthread_attr_t*attr,void*( ...

  7. 2线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(

     1 pthread_create()函数 创建线程 A:依赖的头文件 #include<pthread.h> B:函数声明 int pthread_create(pthread_t ...

  8. 线程基本编程——线程函数大全

    索引: 1.创建线程pthread_create 2.等待线程结束pthread_join 3.分离线程pthread_detach 4.创建线程键pthread_key_create 5.删除线程键 ...

  9. arg是什么函数_怎么实现边听歌边搜图?线程初体验:常用函数

    图控大叔 构图传递思想 阅读从未如此简单!!! 01 前言 今天我们来认识一下线程及其常用函数.当你今天看到我在写推文时,你可能会说: 02 目录总览 2-1 什么是线程2-2 常用函数速览2-3 各 ...

最新文章

  1. Linux客户机访问FTP服务器常见的问题及解决办法
  2. 回归(regression)与分类(classification)的区别
  3. 给Xshell设置舒适的配色方案
  4. AB1601 PWM注意事项
  5. 三点估算pmp_我本人是做项目经理的,我把考PMP也当成一个项目来规划
  6. 在Cloudfoundry上部署RESTful服务
  7. 自己构建React项目
  8. 信息检索:“众筹”专利分析
  9. pygame基础教程
  10. 微波遥感几个重要公式
  11. 【视频】离职创业感悟
  12. 怎样治愈游走性神经痛,游走性神经痛能治愈吗
  13. 什么是telemetry
  14. 阿里云acp证书有用吗?阿里云acp证书考试内容有哪些?
  15. Sikerio --《只狼》
  16. 力扣简单题合集(带答案)
  17. LCD屏幕调试过程(驱动IC ST7701s)
  18. 服务器遇到一个意外的情况,阻止它完成请求
  19. java jacob ocx_JAVA通过jacob调用ocx
  20. 我爱赚钱吧:SEO的好处有哪些?

热门文章

  1. 爱油科技基于SpringCloud的微服务实践
  2. 阿里企业邮箱注册流程(新手指南)
  3. 训练自己的yolo v5模型出现AssertionError: train: No labels(已解决,亲测有效)
  4. GP8101PAC芯片使用及隔离应用
  5. SQL关键字 union、intersect、except用法
  6. html css实现轮播图
  7. 20145209刘一阳 《网络对抗》逆向及BOF基础实践
  8. A facade root has not been set.的解决办法
  9. 语音识别学习资料入门整理
  10. 红烧肉一样的项目管理