当终止运行未分离的 pthread 的进程时,存在已知的内存泄漏。但是,分离线程似乎不是解决方案。考虑以下最小示例:
#include <pthread.h>
#include <stdio.h>
static void* thread(void* _) {
for(;;); return NULL;
}
int main(void) {
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_t tid; pthread_create(&tid, &attr, thread, NULL);
pthread_attr_destroy(&attr);
return 0;
}
创建一个带有无限循环的分离线程并立即终止该进程。根据pthread_detach(3)
,一旦整个进程终止,线程的资源应该自动释放回系统。然而,这显然不是正在发生的事情:
gcc -pthread c.c
valgrind --leak-check=full a.out
==9341== Command: a.out
==9341==
==9341==
==9341== HEAP SUMMARY:
==9341== in use at exit: 272 bytes in 1 blocks
==9341== total heap usage: 1 allocs, 0 frees, 272 bytes allocated
==9341==
==9341== 272 bytes in 1 blocks are possibly lost in loss record 1 of 1
==9341== at 0x4C2ABB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9341== by 0x4012598: _dl_allocate_tls (dl-tls.c:296)
==9341== by 0x4E3C7B5: pthread_create@@GLIBC_2.2.5 (allocatestack.c:579)
==9341== by 0x400825: main (in /home/witiko/a.out)
==9341==
==9341== LEAK SUMMARY:
==9341== definitely lost: 0 bytes in 0 blocks
==9341== indirectly lost: 0 bytes in 0 blocks
==9341== possibly lost: 272 bytes in 1 blocks
==9341== still reachable: 0 bytes in 0 blocks
==9341== suppressed: 0 bytes in 0 blocks
我应该担心吗?在实际程序中,我有几个阻塞线程,因此,就像在最小示例中一样,我不能真正pthread_join()
使用它们。我应该打电话pthread_cancel()
而不是exit()
直接打电话吗?