线程函数退出时候crash

解决办法

线程函数退出之前执行

pthread_exit()


原因分析

pthread_exit()退出当前线程,不退出子线程。

return 退出当前的线程,以及子线程。如果有子线程没有资源释放,就可能导致crash.


man pthread_exit

DESCRIPTION
       The  pthread_exit()  function  terminates the calling thread and returns a value via retval that (if the thread is joinable) is available to
       another thread in the same process that calls pthread_join(3).


       Any clean-up handlers established by pthread_cleanup_push(3)that have not yet been popped, are popped (in the reverse of the order in which
       they  were pushed) and executed.  If the thread has any thread-specific data, then, after the clean-up handlers have been executed, the cor‐
       responding destructor functions are called, in an unspecified order.  终止函数pthread_exit将会call到pthread_cleanup_push,释放临界资源


       When a thread terminates, process-shared resources (e.g., mutexes, condition variables, semaphores, and file descriptors) are not  released,
       and functions registered using atexit(3) are not called.


       After  the  last  thread  in  a process terminates, the process terminates as by calling exit(3) with an exit status of zero; thus, process-
       shared resources are released and functions registered using atexit(3) are called.


参考

pthread_create主线程与创建的新线程之间退出关系

http://mrjake.blog.163.com/blog/static/10510910620124272139947/


pthread_exit与return区别

在ubuntu上调试 stack overflow. pthread_exit在overflow 4个之后才会有错误,return 1个就有

Root Cause

pthread_exit的错误是Segmentation fault,return 的错误是stack corruption detected/stack smashing detected 。

说明return会做检查栈溢出__stack_chk_fail, pthread_exit不会。故有溢出的时候,pthread_exit不会crash,return因为_stack_chk_fail而crash



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值