In my multithreaded application I keep a global per-thread_id table of
thread specific structures. As threads get created I add an entry to
this table. The problem that I am having is when threads go away (exit)
I can't make an explicit call to delete them from my table because of
already defined interface in my application. But then when my table
fills up, I would like to go through it and check if thread[tid] died,
I can free-up that entry and reuse it.
However I can't find any pthread_xxx(pthread_t) function that would
allow me to check wheather the thread is still alive or not.
Does anyone knows the solution to this problem? I would appriciate any
input.
Thanks,
boris
You sure *can* destroy thread-specific data when a thread terminates.
If the thread function passes through you, then you can do it in a
cleanup handler via pthread_cleanup_push()/pthread_cleanup_pop().
If the control does not pass through you---your subsystem is merely
visited by threads to which you attach thread specific data---then
you can use a thread-specific key destructor function to do the cleanup.
Also, thread specific keys allow you to more efficiently locate
thread-specific data than searching by thread ID. You don't need to do anything
with thread ID's in order to keep a table of thread-specific structures.
Look up pthread_key_create() and friends.
>However I can't find any pthread_xxx(pthread_t) function that would
>allow me to check wheather the thread is still alive or not.
There is no need for this in a well designed program. You can pthread_kill()
a thread with signal 0 and check for errors; however this is not reliable.
The thread ID of a terminated thread is not reserved, so the test may indicate
a false positive. So in most circumstances, it's as good as no test at all.
--
#exclude <windows.h>
> In my multithreaded application I keep a global per-thread_id table of
> thread specific structures. As threads get created I add an entry to
> this table.
Be _very_ careful. Nothing stops a thread from going away and another
thread being created with the same ID.
DS