线程如何获取另外主动退出的线程的传出指针
来源:互联网 发布:u8用友软件介绍 编辑:程序博客网 时间:2024/04/30 13:16
1 线程退出函数
#include <pthread.h>
void pthread_exit(void *value_ptr);
2 通过线程id等待指定线程退出的线程函数。
#include <pthread.h>
int pthread_join(pthread_t thread, void **value_ptr);
3 获取线程自身的线程id
#include <pthread.h>
pthread_t pthread_self(void);
下面直接附上代码。
线程2等待线程1退出。并获取 pthread_exit传过来的指针
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/statfs.h>#include <pthread.h>//主动关闭其他线程。//获取指定线程的退出状态static void * proc_func_exit(void *);static void * proc_func_join(void *);int main(){ pthread_t th1 = -1; pthread_t th2 = -1; int ret = -1; ret = pthread_create(&th1,NULL,proc_func_exit,NULL); if(0 != ret) {return -1; } ret = pthread_create(&th2,NULL,proc_func_join,&th1); if(0 != ret) {return -1; } while(1) { sleep(0xfff); } return 0;}static void * proc_func_exit(void *pArg){ // 获取自身的线程id pthread_t pth_id = -1; char *str = NULL; str = (char *)malloc(100); if(NULL == str) {return (void*)0; } pth_id = pthread_self(); printf("\n*** pthread_id = %d****",pth_id); snprintf(str,100,"I am Exit! pthread_id = %d",pth_id ); //线程退出 pthread_exit pthread_exit((void *)str); return (void *)0;}static void * proc_func_join(void *pArg){ char *str = NULL; pthread_t pth_id = -1; if(pArg == NULL) {return (void *)0; } pth_id = *((pthread_t *)(pArg)); pthread_join(pth_id ,&str); if(str != NULL) { printf("\nRecv:%s\n",str); free(str); } return (void *)0;}
0 0
- 线程如何获取另外主动退出的线程的传出指针
- 线程的主动退出机制
- 06-线程的主动切换
- [java多线程]如何安全的退出线程
- 线程的退出
- 线程的安全退出
- 线程的退出
- Spring taskExecutor运行后台线程在Tomcat停止时时主动退出的方法
- ACE的主动线程对象ACE_Task
- 获取线程退出码
- .net C#线程连接Join的技巧,如何真正让一个线程结束,另外一个线程启动
- 保证线程退出的方法
- 线程的创建与退出
- 线程退出死锁的问题解决
- 线程的创建和退出
- 线程的创建与退出
- VC退出线程的方法
- VC线程的退出检测
- Tinker使用过程及注意点(1)-环境搭建及简单使用
- DeepLab:深度卷积网络,多孔卷积 和全连接条件随机场 的图像语义分割 Semantic Image Segmentation with Deep Convolutional Nets, Atro
- 在hbuilder中利用xdebug helper 断点调试php
- scikit-learn的基本用法(四)——数据归一化
- Java对象初始化顺序
- 线程如何获取另外主动退出的线程的传出指针
- leetcode 491. Increasing Subsequences & lc 78. Subsets
- 【Codeforces 803 A】+ 构造
- 求最近点对
- 教你用webgl快速创建一个小世界
- Java设计模式—观察者模式
- 【软考】后缀式
- Win10无法安装.NET Framework 3.5
- traits使用