Update 3
recently, I noticed that my code randomly causes Segmentation Fault errors. But I think that my code is pretty simple so far and I cant figure out where that error comes from. Since it happens randomly, I assume that there is some kind of race-condition. I think this is all the code that might be relevant, tell me if you need more:
namespace thread {
pthread_t terminated_thread_id, /* and others */;
pthread_mutex_t terminate_thread = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t terminate_thread_signal = PTHREAD_COND_INITIALIZER;
int total_thread_count = 0;
int termination; // + sembufs
inline void* Exit(void* value) {
// This must be unlocked after all join-related jobs are done
semop(thread::termination, thread::termination_in_process, 2)
pthread_mutex_lock(&thread::terminate_thread);
thread::terminated_thread_id = pthread_self();
pthread_cond_signal(&thread::terminate_thread_signal);
pthread_mutex_unlock(&thread::terminate_thread);
pthread_exit(value);
return value;
}
}
int main(int argc, const char** argv){
...
pthread_mutex_lock(&thread::terminate_thread);
if(0 != pthread_create(&thread::communication_handler_thread_id, NULL, \
CommunicationHandler, NULL)){
global::PrintDebug("pthread_create() failed", __FILE__, __LINE__);
}
/** 2 more pthread_create()-calls */
do{
thread::terminated_thread_id = pthread_self();
pthread_cond_wait(&thread::terminate_thread_signal, \
&thread::terminate_thread);
if(!pthread_equal(thread::terminated_thread_id, pthread_self())){
pthread_join(thread::terminated_thread_id, NULL);
...
semop(thread::termination, thread::termination_done, 1)
}
}while(thread::total_thread_count > 0);
pthread_mutex_unlock(&thread::terminate_thread);
return 0;
}
The signal terminate_thread_signal is only emitted in the thread::Exit() function. That function is also only called at the end of the function that is used to create the thread.
This is what the debugger shows for the Call Stack:
#0 ( 0xb7fe2424 in __kernel_vsyscall() (??:??)
#1 0xb7fbdfcf __pthread_cond_wait(cond=0x80539c0, mutex=0x8053998) (pthread_cond_wait.c:153)
#2 0x804a094 main(argc=1, argv=0xbffff9c4) (/home/papergay/SeekYourCar/0.2/Server/main.cpp:121)
What I already know is, if the error happens, then no thread has called thread::Exit() yet. I am also using an unnamed namespace with a few initializations (if that might be relevant). I am using Code::Blocks as IDE and GCC as compiler.