I get SEGV in C ++, which I cannot easily reproduce (this happens in about one out of 100,000 test runs) in my call pthread_join()as my application closes. I checked the errno value and it is zero. This works on Centos v4.
Under what conditions pthread_join()will receive SEGV? It may be some kind of race condition, as it is extremely rare. One person suggests that I should not call pthread_detach () and pthread_exit (), but I do not understand why.
My first working hypothesis was that it was pthread_join()called while pthread_exit()it was still working on another thread and that it somehow led to SEGV, but many stated that this was not a problem.
The faulty code that receives SEGV in the main thread during application exit looks something like this (with an error returning the return code for brevity):
return_val = pthread_create(&_threadId, &attr,
(void *(*)(void *))initialize,
(void *)this);
return_val = pthread_detach(_threadId);
releaseCond(mtx(), startCond(), &startCount);
waitOnCond(mtx(), endCond(), &endCount, 0);
pthread_join(_threadId, NULL);
In the child thread that connects when exiting, the following code is executed, which starts at the point above where it releaseCond()is called in the main thread:
waitOnCond(mtx(), startCond(), &startCount);
releaseCond(mtx(), endCond(), &endCount);
pthread_exit(NULL);
The thread appeared correctly and error codes were not created during its creation during application launch, and the thread completed its task correctly, which took about five seconds before the application exited.
What can cause this rare SEGV and how can I program it against it. One complaint is that my pthread_detach () call is a problem, if so, how will my code be fixed.