如何在 C 中正确释放 pthread_t 数组?
How to properly free array of pthread_t in C?
我在 C 中有这样的 pthread_t 数组。
pthread_t *workers; // worker threads running tasks from queue
workers = malloc(sizeof(pthread_t)*workers_count)
// then I creates pthread by passing &workers[i] to pthread_create()
现在我正在考虑如何释放它们。
我做了这样的事情:
for(int i=0; i<workers_count; i++)
free(workers[i]);
free(workers);
但是 pthread_t 不是一个可以包含一些应该释放的内部指针的结构吗?也许有一些功能 pthread_destroy(pthread_t *)?
But isn't pthread_t a struct that can contain some internal pointers
that should be freed?
您不必担心 pthread_t
结构包含什么(或者它是否甚至是 struct
)或它是如何实现的。您(只能)free()
您 使用 malloc()
、calloc()
等
分配的内容
Maybe there is some function pthread_destroy(pthread_t *)?
没有这个功能,因为不需要这个功能
因此,除非您稍后出于任何目的需要线程 ID(加入、使用 pthread_kill()
发送信号等),否则您所做的就可以了。否则,您需要确保在代码中的适当位置执行 free()(即不再需要线程 ID 时)。
我不完全确定你在代码中是如何分配的。这是一个动态分配线程 ID 的简单示例,可能会稍微阐明它。
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void* doSomeThing(void* arg)
{
printf("From thread function: Thread ID: %ld\n", (long)pthread_self());
return NULL;
}
int main(int argc, char *argv[])
{
size_t count = 10;
pthread_t *tid;
tid = malloc(count * sizeof *tid);
for(size_t i = 0; i< count; i++) {
int rc = pthread_create(&tid[i], NULL, &doSomeThing, NULL);
if(rc) { /* failure */ }
}
for(size_t i = 0;i<count; i++) {
pthread_join(tid[i], NULL);
}
free(tid);
return 0;
}
在上面的例子中,我加入线程。由于加入需要线程 ID,因此我 free() 之后 tid
。
此外,您可以看到我只调用了一次 free()
,因为 tid
被分配了 10 个 pthread_t
的块。基本上,每次调用 malloc()
(或 calloc()
或 realloc()
)时,您都会调用 free()
一次,并且传递给 free()
的指针必须是 与先前由 *alloc()
函数之一返回的相同。
我在 C 中有这样的 pthread_t 数组。
pthread_t *workers; // worker threads running tasks from queue
workers = malloc(sizeof(pthread_t)*workers_count)
// then I creates pthread by passing &workers[i] to pthread_create()
现在我正在考虑如何释放它们。 我做了这样的事情:
for(int i=0; i<workers_count; i++)
free(workers[i]);
free(workers);
但是 pthread_t 不是一个可以包含一些应该释放的内部指针的结构吗?也许有一些功能 pthread_destroy(pthread_t *)?
But isn't pthread_t a struct that can contain some internal pointers that should be freed?
您不必担心 pthread_t
结构包含什么(或者它是否甚至是 struct
)或它是如何实现的。您(只能)free()
您 使用 malloc()
、calloc()
等
Maybe there is some function pthread_destroy(pthread_t *)?
没有这个功能,因为不需要这个功能
因此,除非您稍后出于任何目的需要线程 ID(加入、使用 pthread_kill()
发送信号等),否则您所做的就可以了。否则,您需要确保在代码中的适当位置执行 free()(即不再需要线程 ID 时)。
我不完全确定你在代码中是如何分配的。这是一个动态分配线程 ID 的简单示例,可能会稍微阐明它。
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void* doSomeThing(void* arg)
{
printf("From thread function: Thread ID: %ld\n", (long)pthread_self());
return NULL;
}
int main(int argc, char *argv[])
{
size_t count = 10;
pthread_t *tid;
tid = malloc(count * sizeof *tid);
for(size_t i = 0; i< count; i++) {
int rc = pthread_create(&tid[i], NULL, &doSomeThing, NULL);
if(rc) { /* failure */ }
}
for(size_t i = 0;i<count; i++) {
pthread_join(tid[i], NULL);
}
free(tid);
return 0;
}
在上面的例子中,我加入线程。由于加入需要线程 ID,因此我 free() 之后 tid
。
此外,您可以看到我只调用了一次 free()
,因为 tid
被分配了 10 个 pthread_t
的块。基本上,每次调用 malloc()
(或 calloc()
或 realloc()
)时,您都会调用 free()
一次,并且传递给 free()
的指针必须是 与先前由 *alloc()
函数之一返回的相同。