对了pthread_exit回报 [英] pthread_exit vs. return
问题描述
下面我有定义为一个可连接pthread的亚军功能:
无效* sumOfProducts(无效*参数)
{
...
了pthread_exit(0);
}
此线程应该加入主线程。
每当我通过Valgrind的跑到我的节目,我会得到的以下泄漏
泄漏摘要:
失去了肯定:0字节0块
间接丢失:0字节0块
可能丢失:0字节0块
依旧可达:5块968个字节
SUP pressed:0字节0块错误摘要:从0 0上下文错误(SUP pressed:15 10)
我检查了手册页对于pthreads对此表示:
新的线程终止通过以下方式之一: *它调用了pthread_exit(3),指定一个退出状态值,
提供给调用同一进程的另一个线程
在pthread_join(3)。 *它从start_routine返回时()。这等同于调用
了pthread_exit(3)与在返回语句中提供的值。 *它被取消(请参阅pthread_cancel可以(3))。 *任何在进程中的线程的调用出口(3),或主线程
执行从主返回()。这将导致所有的终止
线程中的处理。
不可思议的是,当我更换了pthread_exit()用一个return语句,泄漏消失
收益率(NULL);
我的实际问题是三管齐下:
- 有人能解释为什么return语句给无泄漏?
- 是否有从线程退出这两个语句之间的一些根本的差异,在关系?
- 如果这样,之一,当应pferred比其他$ P $?
以下最低的测试案例表现出你所描述的行为:
的#include< pthreads.h中>
#包括LT&;&unistd.h中GT;void *的APP1(无效* X)
{
睡眠(1);
了pthread_exit(0);
}诠释的main()
{
T1的pthread_t; 在pthread_create(安培; T1,NULL,APP1,NULL);
在pthread_join(T1,NULL); 返回0;
}
的valgrind --leak检查=全--show-可达= YES
显示了从调用的函数分配的5块了pthread_exit()
这是unfreed但进程退出仍可访问。如果了pthread_exit(0);
被替换返回0;
,5块未分配
不过,如果你测试创建和加入大量线程的,你会发现,unfreed内存在的退出,使用量确实的不的增加。这一点,而事实上,它仍然是访问,表明你正在看到的glibc实现的一个怪胎。几个glibc的职能分配与的malloc()
他们是所谓的第一次,这是他们一直分配给进程一生的剩余存储器。 glibc不刻意去自由进程退出此内存,因为它知道该进程正在被拆掉呢 - 它会仅仅是CPU周期的浪费
I have a joinable pthread runner function defined as below:
void *sumOfProducts(void *param)
{
...
pthread_exit(0);
}
This thread is supposed to join the main thread.
Whenever I ran my program through Valgrind I would get the 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)
I checked the man page for pthreads which said:
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.
Miraculously, when I replaced the pthread_exit() with a return statement, the leaks disappeared.
return(NULL);
My actual question is three-pronged:
- Can someone explain why the return statement gave no leaks?
- Is there some fundamental difference between both statements, in relation to exiting from threads?
- If so, when should one be preferred over the other?
The following minimal test case exhibits the behaviour you describe:
#include <pthread.h>
#include <unistd.h>
void *app1(void *x)
{
sleep(1);
pthread_exit(0);
}
int main()
{
pthread_t t1;
pthread_create(&t1, NULL, app1, NULL);
pthread_join(t1, NULL);
return 0;
}
valgrind --leak-check=full --show-reachable=yes
shows 5 blocks allocated from functions called by pthread_exit()
that is unfreed but still reachable at process exit. If the pthread_exit(0);
is replaced by return 0;
, the 5 blocks are not allocated.
However, if you test creating and joining large numbers of threads, you will find that the amount of unfreed memory in use at exit does not increase. This, and the fact that it is still reachable, indicates that you're just seeing an oddity of the glibc implementation. Several glibc functions allocate memory with malloc()
the first time they're called, which they keep allocated for the remainder of the process lifetime. glibc doesn't bother to free this memory at process exit, since it knows that the process is being torn down anyway - it'd just be a waste of CPU cycles.
这篇关于对了pthread_exit回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!