我正在尝试获取一个函数来启动另一个线程,然后返回,线程仍在运行。看起来pthread_detach()会帮我处理内存之类的事情,尽管我不能完全确定。以下是该函数的代码:
void function() {
pthread_mutex_lock(&state.continueLoopingMutex);
if(state.continueLooping == true) {
pthread_mutex_unlock(&state.continueLoopingMutex);
return;
}
state.continueLooping = true;
pthread_mutex_unlock(&state.continueLoopingMutex);
pthread_t thread;
pthread_create(&thread, NULL, runLoop, NULL);
pthread_detach(thread);
}(当continueLooping设置为false时,循环将停止,一旦循环结束,它只会返回,不做其他任何事情)。
这个结构有没有可能导致内存泄漏等我应该关心的错误?
谢谢
发布于 2011-05-15 22:33:52
我看不出有什么问题。
是的,pthread_detach将负责释放线程的资源。
发布于 2011-05-15 22:50:27
如果您只想让线程继续同时运行,那么无论您是否分离线程都不会有什么不同。但从分离的线程中收集资源(如返回值)将变得更加困难或不那么惯用。是的,这段代码不会暴露任何内存泄漏。
https://stackoverflow.com/questions/6008956
复制相似问题