printf("thread = %d\n",pthread_kill(thread_id,0));
上述用法,在未对一个pthread_t执行pthread_create前调用pthread_kill,会出现Segmentation fault
google上摘录如下解释:
pthread_t
is not a thread ID, or a numeric index. It is an opaque type. Making up values can result in a crash.
On Linux NPTL, pthread_t is used as a pointer:
int
__pthread_kill (threadid, signo)
pthread_t threadid;
int signo;
{
struct pthread *pd = (struct pthread *) threadid;
It should be fairly clear where things are going wrong already :) Note that this pointerness is also an implementation detail - the older Linuxthreads implementation used numeric indices into a table, and there you could indeed make up TIDs and not expect things to crash.
You need to be tracking thread life and death yourself. A pthread_t
is valid until you call pthread_join
on it successfully. If you want to test whether a valid pthread_t
is alive, call pthread_tryjoin_np
on it; if it returns EBUSY
, the thread is alive. If the function succeeds, the pthread_t
is no longer valid; you must not re-use it at this point - so you must make a note somewhere that that thread is dead now, and there's no need to check it anymore!
You could, of course, implement your own tracking system - create a table somewhere of liveness, a system for handing out TIDs, and passing them into newly created threads. Have each thread mark itself as dead prior to exiting (perhaps using pthread_cleanup_push
so you handle thread cancellation and pthread_exit
), and detach the thread so you don't need to join it (using pthread_detach
). Now you have explicit control of your thread-death reporting.