Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to determine if thread has died having its pthread_t tid?

601 views
Skip to first unread message

Boris Bak

unread,
Apr 6, 2000, 3:00:00 AM4/6/00
to
Hi,

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


Kaz Kylheku

unread,
Apr 6, 2000, 3:00:00 AM4/6/00
to
On Thu, 06 Apr 2000 15:33:10 -0500, Boris Bak <bori...@ni.com> wrote:
>Hi,
>
>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.

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>

David Schwartz

unread,
Apr 6, 2000, 3:00:00 AM4/6/00
to

Boris Bak wrote:

> 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

0 new messages