【发布时间】:2025-11-23 10:30:01
【问题描述】:
我正在尝试创建一个分离的线程,因此我不需要释放为其分配的内存。 Valgrind 用于检查内存泄漏。 我用过IBM example 并写道:
void *threadfunc(void *parm)
{
printf("Inside secondary thread\n");
return NULL;
}
int main(int argc, char **argv)
{
pthread_t thread;
int rc=0;
rc = pthread_create(&thread, NULL, threadfunc, NULL);
sleep(1);
rc = pthread_detach(thread);
return 0;
}
此方法有效且不会造成泄漏,而是没有“sleep(1);”的版本没有。 为什么需要这个 sleep(1)?
【问题讨论】:
标签: c multithreading memory-leaks pthreads