我在C中编写了一个程序。在for循环中它创建了12个线程。
for (i = 0; i < 12; i++)
{
status=pthread_create(&ntid[i],NULL,th_f,NULL);
if (status != 0)
{
printf("Error in Creating Thread\n");
exit(1);
}
}
然后我按照所示加入了他们:
for (i = 0; i < 12; i++)
{
ret=pthread_join(ntid[i],&retval);
if (ret)
{
printf("Error joining\n");
exit(1);
}
printf("Thread terminated with status %d\n", *((int*)retval));
free (retval);
}
线程正在执行的函数是th_f
。它有1个malloc,它在加入后被释放(正如你在`free(retval)中看到的那样;
关于该计划的valgrind报告如下:
==27123== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 1)
==27123== malloc/free: in use at exit: 1,552 bytes in 5 blocks.
==27123== malloc/free: 29 allocs, 24 frees, 4,864 bytes allocated.
==27123== For counts of detected errors, rerun with: -v
==27123== searching for pointers to 5 not-freed blocks.
==27123== checked 89,896 bytes.
和
==27123== LEAK SUMMARY:
==27123== definitely lost: 0 bytes in 0 blocks.
==27123== possibly lost: 0 bytes in 0 blocks.
==27123== still reachable: 1,552 bytes in 5 blocks.
==27123== suppressed: 0 bytes in 0 blocks.
所以我需要回答这些问题:
答案 0 :(得分:0)
额外的分配和释放可能是线程堆栈。这五个额外的内容可能是由线程库在内部使用的 - 正如它所说的那样,它们不会被泄露但仍然可以访问。