假设我有以下代码:
while(TRUE) {
pthread_t *thread = (pthread_t *) malloc(sizeof(pthread_t));
pthread_create(thread, NULL, someFunction, someArgument);
pthread_detach(*thread);
sleep(10);
}
分离的线程是否会释放 malloc 分配的内存,或者这是我现在必须做的事情?
最佳答案
没有。 pthread_create() 无法知道传递给它的thread 指针是动态分配的。 pthreads 不在内部使用这个值;它只是将新的线程 ID 返回给调用者。您不需要动态分配该值;您可以改为传递局部变量的地址:
pthread_t thread;
pthread_create(&thread, NULL, someFunction, someArgument);
关于c - pthread_detach 会为我管理我的内存吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/135458/