linux c编程:线程退出
在线程创建的时候pthread_exit都是调用的固定参数,我们先来看下如果用自动变量作为pthread_exit的参数时出现的问题
typedef struct foo{
int a;
int b;
int c;
int d;
}foo;
void printinfo(const char *s,const struct foo *fp){
printf("%s",s);
printf("structure at 0x%lx\n",(unsigned long)fp);
printf("foo.a=%d\n",fp->a);
printf("foo.b=%d\n",fp->b);
printf("foo.c=%d\n",fp->c);
printf("foo.d=%d\n",fp->d);
}
void printids(const char *s){
pid_t pid;
pthread_t tid;
pid=getpid();
tid=pthread_self();
printf("%s pid %lu tid %lu (0x%lx)\n",s,(unsigned long)pid,(unsigned long)tid,(unsigned long)tid);
}
void *thr_fn2(void *arg){
printf("threaqd 2:ID is %lu\n",(unsigned long)pthread_self());
pthread_exit((void *)0);
}
void *thr_fn1(void *arg){
foo f={1,2,3,4};
printinfo("thread1:\n",&f);
pthread_exit((void *)&f);
}
int main()
{
pthread_t tid1,tid2;
foo *fp;
pthread_create(&tid1,NULL,thr_fn1,NULL);
pthread_join(tid1,(void *)&fp);
Sleep(1);
printf("parent starting create thread2:\n");
pthread_create(&tid2,NULL,thr_fn2,NULL);
Sleep(1);
printinfo("parent:\n",fp);
return 0;
}
在这个程序中,调用pthread_join(tid1,(void *)&fp)的时候会将返回码赋值给fp参数。然后再父进程中调用fp。
执行结果:可以看到当父进程中试图访问已退出的第一个线程传给它的结构时,内存不在有效,因此得到了SIGSEGV信号
Thread1:
Structure at 0x2bfff04
foo.a=1
foo.b=2
foo.c=3
foo.d=4
parent starting create thread2
thread 2 : ID is 3
segmentation fault(core dumped)
线程可以安排它退出时需要调用的函数,这样的函数称为线程清理处理程序。
void thread_cleanup_push(void (*rtn)(void *), void *arg);
void pthread_cleanup_pop(int execute)
rtn是调用的函数,arg是传入的参数。
void cleanup(void *arg){
printf("cleanup:%s\n",(char *)arg);
}
void *thr_fn2(void *arg){
printf("thread2 start\n");
pthread_cleanup_push(cleanup,"thread2 first handler");
pthread_cleanup_push(cleanup,"thread2 second handler");
printf("thread2 push complete\n");
if (arg)
pthread_exit((void *)2);
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
}
void *thr_fn1(void *arg){
printf("thread1 start\n");
pthread_cleanup_push(cleanup,"thread1 first handler");
pthread_cleanup_push(cleanup,"thread1 second handler");
printf("thread1 push complete\n");
if (arg)
return((void *)1);
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
}
int main()
{
pthread_t tid1,tid2;
void *tret;
pthread_create(&tid1,NULL,thr_fn1,(void *)1);
pthread_create(&tid2,NULL,thr_fn2,(void *)1);
pthread_join(tid1,&tret);
printf("thread 1 exit code %ld\n",(long)tret);
pthread_join(tid2,&tret);
printf("thread 2 exit code %ld\n",(long)tret);
return 0;
}
运行结果如下:两个线程都正确的自动启动和退出了,但是只有第二个线程的清理处理程序被调用了,如果线程是通过从它的启动历程中返回(return)而不是pthread_exit的话,它的清理程序就不会调用

linux c编程:线程退出的更多相关文章
- Linux 多线程编程--线程退出
今天分析项目中进程中虚存一直增长问题,运行10个小时虚存涨到121G ,RSS占用为16G 非常恐怖. Valgrind测试无内存泄漏. 内存32G 64bit系统信息如下: Linux线程使用方式是 ...
- Linux多线程编程——线程的创建与退出
POSIX线程标准:该标准定义了创建和操纵线程的一整套API.在类Unix操作系统(Unix.Linux.Mac OS X等)中,都使用Pthreads作为操作系统的线程.Windows操作系统也有其 ...
- Linux多任务编程——线程
线程基础 △ 由于进程的地址空间是私有的,因此在进行上下文切换时,系统开销比较大 △ 在同一个进程中创建的线程共享该进程的地址空间 △ 通常线程值得是共享相同地址空间的多个任务 △ 每个线程的私有这些 ...
- Linux系统编程——线程私有数据
在多线程程序中.常常要用全局变量来实现多个函数间的数据共享.因为数据空间是共享的,因此全局变量也为全部线程共同拥有. 測试代码例如以下: #include <stdio.h> #inclu ...
- linux系统编程--线程
安装线程man page,命令:sudo apt-get install manpages-posix-dev 线程概念 什么是线程 LWP:light weight process 轻量级的进程,本 ...
- linux系统编程--线程同步
同步概念 所谓同步,即同时起步,协调一致.不同的对象,对“同步”的理解方式略有不同. 如,设备同步,是指在两个设备之间规定一个共同的时间参考: 数据库同步,是指让两个或多个数据库内容保持一致,或者按需 ...
- Linux系统编程 —线程同步概念
同步概念 同步,指对在一个系统中所发生的事件之间进行协调,在时间上出现一致性与统一化的现象. 但是,对于不同行业,对于同步的理解略有不同.比如:设备同步,是指在两个设备之间规定一个共同的时间参考:数据 ...
- Linux——网络编程线程池机制
#include <stdlib.h>#include <pthread.h>#include <unistd.h>#include <assert.h> ...
- LInux多线程编程----线程特定数据的处理函数
1.pthread_key_t和pthread_key_create() 线程中特有的线程存储, Thread Specific Data .线程存储有什么用了?他是什么意思了?大家都知道,在多线程程 ...
- Linux系统编程 —线程属性
在之前的章节中,我们在调用pthread_create函数创建线程时,第二个参数(即线程属性)都是设为NULL,即使用默认属性.一般情况下,使用默认属性已经可以解决我们开发过程中的大多数问题. 但是, ...
随机推荐
- 遍历修改django bootstrap form 为 django bootstrap3
#!/usr/bin/env python # encoding: utf-8 import re import os fname = '' bt_pattern = re.compile(r'{% ...
- 字典转模型的过程中,空值和id特殊字符的处理
在IOS 中id是特殊字符,可是非常多时候从网络中下载的数据是以id保存的 假设在定义属性的时候 @property(nonatomic, copy) NSString *id; 就不会出现错误 当键 ...
- 二维树状数组的区间加减及查询 tyvj 1716 上帝造题的七分钟
详细解释见小结.http://blog.csdn.net/zmx354/article/details/31740985 #include <algorithm> #include < ...
- java.lang.NoSuchMethodError: org.springframework.beans.factory.config.ConfigurableBeanFactory.getSingletonMutex()Ljava/lang/Object
© 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述 搭建SSH框架,没有添加事务时一切正常,最后添加完事务后报错,并且怎么弄都是一样.报错信息如下: 警告: Exception encou ...
- android电话状态的监听
电话状态的监听: /*** * 继承PhoneStateListener类,我们能够又一次其内部的各种监听方法 然后通过手机状态改变时,系统自己主动触发这些方法来实现我们想要的功能 */ class ...
- 深入理解Tomcat系列之五:Context容器和Wrapper容器
前言 Context容器是一个Web项目的代表,主要管理Servlet实例,在Tomcat中Servlet实例是以Wrapper出现的.如今问题是怎样才干通过Context容器找到详细的Servlet ...
- Jmeter拓展插件(jmeter-plugins)
Jmeter是一款开源的性能测试工具,纯java编写,体积小,功能强大,基本可以满足性能测试需求.另Jmeter还右一系列的插件来增强其功能,插件地址jmeter-plugins.org.插件现在有5 ...
- CocoaPods报错:The dependency `` is not used in any concrete target
内容提要: podfile升级之后到最新版本,pod里的内容必须明确指出所用第三方库的target,否则会出现The dependency `` is not used in any concrete ...
- protobuf编译安装
为什么选择protobuf,而不选择thift和avro,原因大概几点吧,网上对比的文章很多,我主要关注以下几点 1.protobuf序列化性能最好,序列化后字节数最少. 2.protobuf是单纯的 ...
- named主从环境部署
named主 1. bind服务安装配置 yum -y install bind*.x86_64 配置文件: /etc/named.conf /etc/named.rfc1912.zones /etc ...