未在 C 中正确释放线程

Not deallocating properly threads in C

我正在用 C 编写一个程序,它使用 threads,我永远不知道它们何时完成,所以我使用 pthread_detach() 来释放内存,对吗?

代码如下:

pthread_t thread_id_Gruder, thread_id_Tavish;

estado_threadGruder = pthread_create(&thread_id_Gruder,NULL,SERVER_McGruderDedicado,&td_Gruder);

estado_threadTavish = pthread_create(&thread_id_Tavish,NULL,SERVER_McTavishDedicado,&td_Tavish);

if (estado_threadGruder != 0 || estado_threadTavish != 0) {
            if (estado_threadGruder != 0) MSSG_error(THREAD_ERROR, "McGruder");
            else MSSG_error(THREAD_ERROR, "McTavish");
            raise(SIGINT);
            pause();
}
pthread_detach(thread_id_Gruder);
pthread_detach(thread_id_Tavish);

但是当我使用 valgrind 检查内存泄漏时,我发现了这个输出:

HEAP SUMMARY:
==19426==     in use at exit: 544 bytes in 2 blocks
==19426==   total heap usage: 15 allocs, 13 frees, 628 bytes allocated
==19426== 
==19426== Thread 1:
==19426== 272 bytes in 1 blocks are possibly lost in loss record 1 of 2
==19426==    at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19426==    by 0x40134A6: allocate_dtv (dl-tls.c:286)
==19426==    by 0x40134A6: _dl_allocate_tls (dl-tls.c:530)
==19426==    by 0x4E44227: allocate_stack (allocatestack.c:627)
==19426==    by 0x4E44227: pthread_create@@GLIBC_2.2.5 (pthread_create.c:644)
==19426==    by 0x1097AA: main (in /users/home/alumnes/LS//PRACTICA/Lionel-S/Lionel)
==19426== 
==19426== 272 bytes in 1 blocks are possibly lost in loss record 2 of 2
==19426==    at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19426==    by 0x40134A6: allocate_dtv (dl-tls.c:286)
==19426==    by 0x40134A6: _dl_allocate_tls (dl-tls.c:530)
==19426==    by 0x4E44227: allocate_stack (allocatestack.c:627)
==19426==    by 0x4E44227: pthread_create@@GLIBC_2.2.5 (pthread_create.c:644)
==19426==    by 0x1097CC: main (in /users/home/alumnes/LS//PRACTICA/Lionel-S/Lionel)
==19426== 
==19426== LEAK SUMMARY:
==19426==    definitely lost: 0 bytes in 0 blocks
==19426==    indirectly lost: 0 bytes in 0 blocks
==19426==      possibly lost: 544 bytes in 2 blocks
==19426==    still reachable: 0 bytes in 0 blocks
==19426==         suppressed: 0 bytes in 0 blocks
==19426== 
==19426== For counts of detected and suppressed errors, rerun with: -v
==19426== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)

我尝试 运行 没有 phtread_detach() 函数的代码,但 valgrind 显示相同的内存泄漏,所以我假设我没有正确分离...

我是否正确地分离了线程,或者问题可能出在我的线程中?

谢谢。

问题很可能是您的线程。即使您没有在其中使用任何 malloc

事情是这样的,当您使用 pthread_create 创建线程时,库会为线程的正确操作分配一堆东西,并启动它 运行ning。通常,这些东西只有在使用 pthread_join 加入这些线程时才会被释放。但是,在某些情况下,在您的代码中没有必要同步线程,而只是希望它们在完成后消失。这就是为什么有 pthread_detach。分离的线程将在其函数 returns 后立即清除自身。注意,当他们 return.

如果程序结束时您的线程还没有结束,您的主函数将正常结束,但您的线程仍在 运行ning。由于它们还没有 returned,所以它们还没有被清理干净。

如果你打算让这些线程运行直到程序完成,或者想在程序结束时等待它们没有完成,那么你确实有一点想要同步它们(结束程序的)和 pthread_detach 可能不是要走的路。

如果您的线程有无限循环,我建议创建一个从 0 开始的变量 should_Terminate,如果它的值为 1,则让线程中的循环中断。然后您可以在需要时将其设置为 1终止您的程序,并使用 pthread_join 等待线程正常结束它们的工作。

如果他们没有无限循环,并且肯定会在某个时候return,那么在程序结束时加入它们就足够了。