我有一个可连接的pthread runner函数,定义如下:
void *sumOfProducts(void *param)
{
...
pthread_exit(0);
}
这根线应该连接主线.
每当我通过Valgrind运行我的程序时,我都会得到following leaks分:
LEAK SUMMARY:
definitely lost: 0 bytes in 0 blocks
indirectly lost: 0 bytes in 0 blocks
possibly lost: 0 bytes in 0 blocks
still reachable: 968 bytes in 5 blocks
suppressed: 0 bytes in 0 blocks
ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 10)
我查看了手册页上的pthreads,上面写着:
The new thread terminates in one of the following ways:
* It calls pthread_exit(3), specifying an exit status value that is
available to another thread in the same process that calls
pthread_join(3).
* It returns from start_routine(). This is equivalent to calling
pthread_exit(3) with the value supplied in the return statement.
* It is canceled (see pthread_cancel(3)).
* Any of the threads in the process calls exit(3), or the main thread
performs a return from main(). This causes the termination of all
threads in the process.
神奇的是,当我用返回语句the leaks disappeared替换pthread_exit()时.
return(NULL);
我的实际问题有三个方面:
- 有人能解释为什么返回声明没有泄露信息吗?
- 在退出线程方面,这两个语句之间是否存在一些基本的区别?
- 如果是这样,什么时候应该 Select 其中一个?