解决办法
线程函数退出之前执行
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