我在 C++ 中获得了一个 SEGV,pthread_join()
当我的应用程序关闭时,我在调用时无法轻松重现(它发生在大约 100,000 次测试运行中)。我检查了 errno 的值,它是零。这是在 Centos v4 上运行的。
在什么条件下会pthread_join()
获得 SEGV?这可能是某种竞争条件,因为它极为罕见。有人建议我不应该调用 pthread_detach() 和 pthread_exit(),但我不清楚为什么。
我的第一个工作假设是在另一个线程中仍在运行时pthread_join()
被调用,pthread_exit()
并且这会以某种方式导致 SEGV,但许多人表示这不是问题。
在应用程序退出期间在主线程中获取 SEGV 的失败代码大致如下所示(为简洁起见,省略了错误返回代码检查):
// During application startup, this function is called to create the child thread:
return_val = pthread_create(&_threadId, &attr,
(void *(*)(void *))initialize,
(void *)this);
// Apparently this next line is the issue:
return_val = pthread_detach(_threadId);
// Later during exit the following code is executed in the main thread:
// This main thread waits for the child thread exit request to finish:
// Release condition so child thread will exit:
releaseCond(mtx(), startCond(), &startCount);
// Wait until the child thread is done exiting so we don't delete memory it is
// using while it is shutting down.
waitOnCond(mtx(), endCond(), &endCount, 0);
// The above wait completes at the point that the child thread is about
// to call pthread_exit().
// It is unspecified whether a thread that has exited but remains unjoined
// counts against {PTHREAD_THREADS_MAX}, hence we must do pthread_join() to
// avoid possibly leaking the threads we destroy.
pthread_join(_threadId, NULL); // SEGV in here!!!
在退出时加入的子线程运行以下代码,该代码从上面在releaseCond()
主线程中调用的位置开始:
// Wait for main thread to tell us to exit:
waitOnCond(mtx(), startCond(), &startCount);
// Tell the main thread we are done so it will do pthread_join():
releaseCond(mtx(), endCond(), &endCount);
// At this point the main thread could call pthread_join() while we
// call pthread_exit().
pthread_exit(NULL);
该线程似乎正常启动,并且在应用程序启动期间创建期间没有产生错误代码,并且线程正确执行了它的任务,这在应用程序退出前大约需要五秒钟。
什么可能导致这种罕见的 SEGV 发生,我该如何防御性地编程。一种说法是我对 pthread_detach() 的调用是问题所在,如果是这样,我的代码应该如何更正。