我有一个可连接的pthread运行器函数,定义如下:
void *sumOfProducts(void *param)
{
...
pthread_exit(0);
}
Run Code Online (Sandbox Code Playgroud)
该线程应该加入主线程.
每当我通过Valgrind运行我的程序时,我会得到以下泄漏:
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)
Run Code Online (Sandbox Code Playgroud)
我检查了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.
Run Code Online (Sandbox Code Playgroud)
奇迹般地,当我用return语句替换pthread_exit()时,泄漏消失了.
return(NULL);
Run Code Online (Sandbox Code Playgroud)
我的实际问题是三管齐下的:
caf*_*caf 40
以下最小测试用例展示了您描述的行为:
#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;
}
Run Code Online (Sandbox Code Playgroud)
valgrind --leak-check=full --show-reachable=yes显示从被调用的函数分配的5个块是不pthread_exit()同意的但在进程退出时仍可到达.如果pthread_exit(0);替换为return 0;,则不分配5个块.
但是,如果您测试创建并加入大量线程,您会发现退出时使用的未同步内存量不会增加.这个以及它仍然可以访问的事实表明你只是看到了glibc实现的奇怪之处.几个glibc函数malloc()在第一次调用时分配内存,它们在进程生命周期的剩余时间内保持分配.glibc并不打算在进程退出时释放这个内存,因为它知道进程正在被拆除 - 它只是浪费CPU周期.
小智 11
不确定你是否仍然对此感兴趣,但我目前正在调试类似的情况.使用的线程pthread_exit会导致valgrind报告可访问的块.原因似乎在这里得到了很好的解释:
https://bugzilla.redhat.com/show_bug.cgi?id=483821
从本质上讲,它似乎pthread_exit会导致dlopen在进程退出时从未明确清除的内容.
| 归档时间: |
|
| 查看次数: |
24780 次 |
| 最近记录: |