目录

  • 一 回顾
  • 二 回收子线程
  • 三 在栈区定义回收子线程的变量
  • 四 在堆区定义回收子线程的变量
  • 五 取消(杀死)线程

一 回顾

用了 exit(0);进程退出方式和 pthtread_exit(NULL);的混合使用
证明了 pthtread_exit(NULL);退出单个线程是成功的 可行的 。

二 回收子线程

还是在上次的基础 加 pathread_join函数 即可

参数 第一个 :线程id
参数 第二个:函数指针  (二级指针 )
二级指针的赋值对象可以是这样的

比如

int **A;  //二级指针
int *p;  //一级指针
A=&p;   //赋值过程

三 在栈区定义回收子线程的变量

#include<stdio.h>
#include <pthread.h>
#include<string.h>void *mychild(void * arg)
{int h=0;printf("child pthred_create id is %ld",pthread_self());while(h<10){h++;printf("child h===%d\n",h)lsleep(1);if(h==3){int num=111;pthread_exit(&num);   }}}int main()
{int returns;int i=0;pthread_t pthid;/* &mychild  mychild   都可以 *//* 1 &mychild :mychild 表示是一个指针 赋值给二级指针 就需要 & mychild */ /*  mychild :本来就是一个地址 也可以直接赋值 */  returns=pthread_create(&pthid,NULL,&mychild,NULL);//创建 线程 // mychild 克隆子线程 printf("peranst pthred_create id is %ld",pthread_self());printf("return information is %s\n",strerror(returns)); void *str=NULL; //函数指针变量初始化赋值pthread_join(pthid,&(str));//回收子线程函数 printf("num===%d\n",*(int *)str);//回收子线程退出携带的值 while(i<10){i++;printf("parenst i====%d\n",i);sleep(1);}return 0;
}


出现随机数了

因为你在栈区定义的  是不被共享的 所以值对应不上

四 在堆区定义回收子线程的变量

如果在堆区呢
#include<stdio.h>
#include <pthread.h>
#include<string.h>int num=111;  //堆区定义
void *mychild(void * arg)
{int h=0;printf("child pthred_create id is %ld",pthread_self());while(h<10){h++;printf("child h===%d\n",h)lsleep(1);if(h==3){pthread_exit(&num);   }}}int main()
{int returns;int i=0;pthread_t pthid;/* &mychild  mychild   都可以 *//* 1 &mychild :mychild 表示是一个指针 赋值给二级指针 就需要 & mychild */ /*  mychild :本来就是一个地址 也可以直接赋值 */  returns=pthread_create(&pthid,NULL,mychild,NULL);//创建 线程 // mychild 克隆子线程 printf("peranst pthred_create id is %ld",pthread_self());printf("return information is %s\n",strerror(returns)); void *str=NULL; //函数指针变量初始化赋值pthread_join(pthid,&(str));//回收子线程函数 printf("num===%d\n",*(int *)str);//回收子线程退出携带的值 while(i<10){i++;printf("parenst i====%d\n",i);sleep(1);}return 0;
}


也就是验证了 retval 指针必须指向 全局 堆区
这样回收的值 才会正常。

五 取消(杀死)线程


一  pthred_cancel函数使用方式1 直接在子线程 定义三个变量 并且必须做一次系统调用 2 也就是 那句话 先到了系统调用 父线程开始回收先到的 3 这里只有一句话 所以就是本身

如果不确定

直接把这里设置为取消点
取消点理解一个标志位
#include<stdio.h>
#include <pthread.h>
#include<string.h>void *mychild(void * arg)
{int h=0;printf("child pthred_create id is %ld",pthread_self());while(h<10){int a=11;int b=22;int  c=2222;printf("a==%d b==%d c==%d\n",a,b,c);pthread_testcancel();//确定取消点 }}int main()
{int returns;int i=0;pthread_t pthid;/* &mychild  mychild   都可以 *//* 1 &mychild :mychild 表示是一个指针 赋值给二级指针 就需要 & mychild */ /*  mychild :本来就是一个地址 也可以直接赋值 */    returns=pthread_create(&pthid,NULL,mychild,NULL);//创建 线程 // mychild 克隆子线程 printf("peranst pthred_create id is %ld",pthread_self());printf("return information is %s\n",strerror(returns)); void *str=NULL; //函数指针变量初始化赋值pthread_join(pthid,&(str));//回收子线程函数 printf("num===%d\n",*(int *)str);//回收子线程退出携带的值 while(i<10){i++;printf("parenst i====%d\n",i);sleep(1);}return 0;
}
 printf 为系统调用函数 也就是 输出了 那么父线程开始回收 子进程的while(1)的内容

一直在打印  并且后台生成了 ./a.out
kill  -9 7480  (杀死子进程 父进程无法回收了)

ps aux  查看 是否还存在./a.out

验证成功  不存在了

linux 回收子线程 和取消(杀死)线程相关推荐

  1. Linux系统编程---14(回收子线程,回收多个子线程,线程分离,杀死线程)

    回收子线程 pthread_join 函数 阻塞等待线程退出,获取线程退出状态 其作用,对应进程中 waitpid() 函数. int pthread_join (pthread_t thread,v ...

  2. 线程分离属性,线程取消(状态,类型),线程取消例程函数

    目录 1.线程分离属性 2.线程取消(状态,类型) 3.线程取消例程函数 1.线程分离属性 默认情况下,线程启动后处于可接合状态(即未分离),此时的线程可以在退出时让其他线程接合以便释放资源,但若其他 ...

  3. pthread 立即停止线程_pthread线程的终止退出 | 线程的大量创建

    1. 线程只是从启动例程中返回,返回值是线程的退出码: 2. 线程调用了pthread_exit函数: 3. 线程可以被同一进程中的其他线程取消. ************************** ...

  4. Linux学习之系统编程篇:回收子线程资源

    子线程退出后,主控线程也是需要回收子线程资源的. 函数:pthread_ join 阻塞等待线程退出,获取线程退出状态函数说明: int pthread_join(pthread_t thread, ...

  5. linux取消线程的原理,linux线程的取消(终止)方法

    关键: pthread_cancel函数发送终止信号 pthread_setcancelstate函数设置终止方式 pthread_testcancel函数取消线程(另一功能是:设置取消点) 1 线程 ...

  6. linux杀死线程函数,Linux线程-pthread_kill

    该函数可以用于向指定的线程发送信号: int pthread_kill(pthread_t threadId,int signal); 如果线程内不对信号进行处理,则调用默认的处理程式,如SIGQUI ...

  7. 【Linux】应用篇九--线程的取消与互斥

    应用篇九--线程的取消与互斥 一.线程的取消 二.线程的清理 三.线程的互斥和同步 四.互斥锁的创建与与销毁 1.互斥锁的创建 2.互斥锁的销毁 3.互斥锁的使用 五.读写锁 六.死锁 一.线程的取消 ...

  8. 【Linux系统编程】守护进程、线程

    ------------->[Linux系统编程/网络编程](学习目录汇总) <-------------- 目录 1.守护进程 1.1 进程组 1.2 会话 1.3 setsid()函数 ...

  9. Linux系统编程08---守护进程与线程

    目录 作者介绍 学习目标 1 守护进程(也称Daemon 精灵进程-->是后台服务进程) 1.1 守护进程的特点 1.2 进程组和会话 1.3 创建守护进程的模型 1.4 实例:创建守护进程模型 ...

最新文章

  1. Transformer-LS霸榜ImageNet,输入长度提升三倍!极度压缩参数
  2. Matlab调用函数实现CIC滤波器
  3. spring 整和activemq
  4. redmine 邮件发送问题修复
  5. vue2.0 -- watch监听
  6. C++中有关queue常用函数的用法及其注意要项
  7. 开源作者在行动:疫情防控相关开源项目推荐
  8. Spring MVC 基础笔记
  9. linux shell的组成,理解Linux中子shell的概念
  10. 关于OAuth2.0的文章收集
  11. 【转】ACE开发环境搭建
  12. jedate选中日期后关闭弹层_jeDate日期控件的使用以及选中后点确定按钮关闭功能...
  13. 软件工程毕业设计要求
  14. [单片机框架][bsp层][cx32l003][bsp_system_clock] clock配置和使用
  15. 小众软件(持续更新)
  16. Ubuntu20.04使用清华源下载Qt
  17. QT 错误:Unable to create a debugging engine解决
  18. 【算法设计与分析】经典常考三十三道例题AC代码
  19. 陀螺研究院 | 产业区块链发展周报(12.19—12.25)
  20. win7系统设备管理器打开后一片空白怎么办

热门文章

  1. 一起学python爬虫第二天
  2. 线程池ThreadPool
  3. 瓶颈期这样做更易度过(上)
  4. linux服务器系统简述
  5. mac osx 装oracle 11g,macOS 安装Oracle数据库
  6. document.getElementById(id)的用法
  7. Postgresql数据库连接池
  8. 什么是对数器?对数器的作用是什么?
  9. Python3.11最新版亲测结果
  10. 《仿人机器人原理与实战》一2.3 行为链实验入门