我写了一个简单的线程程序:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdint.h>
#define THREADS 5
void* HelloWorld(void *t)
{
printf("Thread ID #%lu: (%lu) Hello World !!\n", pthread_self(), (unsigned long)t);
return NULL;
}
int main()
{
pthread_t thread[THREADS];
uint32_t i;
int err;
for(i = 0; i < THREADS; ++i)
{
err = pthread_create(&thread[i], NULL, &HelloWorld, (void*)(unsigned long long)i);
if(err != 0)
{
printf("Error %d: Thread %d Creation Unsuccessful !!\n", err, i);
}
printf("Thread %lu in main()\n", pthread_self());
}
/*
for(i = 0; i < THREADS; ++i)
{
pthread_join(thread[i], NULL); // Error checking implemented
}
*/
return 0;
}
但是在使用valgrind作为:
valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./hello
pthread_join()
无论在程序中使用与否,它都会显示相同的内存使用/泄漏输出。
请解释这种行为,因为我在这里读到:
pthread_join() 或 pthread_detach() 函数最终应为使用 detachstate 属性设置为 PTHREAD_CREATE_JOINABLE 创建的每个线程调用,以便可以回收与线程关联的存储。
如果我不打电话,如何回收存储pthread_join()