Subj : Re: problem with pthread_kill To : comp.programming.threads From : Joe Seigh Date : Sat Jul 16 2005 08:08 am martinacevedo@gmail.com wrote: > Hello, I´m implementing an multithread application under linux with > gcc and kdevelop. I use pthread library from linux to handle threads > and mutex. My question is how can my program detect if a thread struct > is still valid or active.... I use a library that call a function > pthread_kill (threadID,0) in order to detect if a thread is alive but > when the thread is invalid(I thinks is invalid), it causes a > segmentation fault, the program runs on mandriva linux xeon p4 dual > processor. there is another function that can detect the validity of > the struct > That's pretty easy to fix. Don't use pthread_kill. Using pthread_kill to determine whether a thread "is alive", or kill to determine whether a process is alive, is meaningless. Using signals is only meaningful if you know the thread or process is alive a priori. All the return code from pthread_kill tells you is whether the pthread id is valid at the time or not. It doesn't tell you whether the thread is alive or even if the pthread id refers to the same thread that it thought it did. Pthread ids are only valid for joinable threads that you haven't executed a pthread_join for. Since an indefinite amount of time can pass between the time a particular thread executes a pthread_exit and the time a pthread_join is done during which phtread_kill will work fine, pthread_kill doesn't do what you think it does. You have to use explicit indicators set by the threads indicating that they're alive and possible mutexes and condition variables to access them if needed. -- Joe Seigh When you get lemons, you make lemonade. When you get hardware, you make software. .