[Ilugc] Thread status (pthread library).

Praveen Kumar praveen at symonds.net
Tue Jun 20 12:32:14 IST 2006


knaren at midascomm.com wrote:

> I'm using pthread library to write a threaded appilcation. 
> And for long i have been wondering, if there is some method 
> through which i can know the state of the thread - alive, 
> running, blocked etc. More precisely, is there any function 
> in pthread library which returns indicating if a thread is 
> alive/cancelled/exited ??
> 
> Searching the net gave me some interesting stuffs, but not 
> what i intended.

I had a similar requirement sometime ago. I used pthread_kill with
signal number 0 on the thread that needs to be tested. If the return
value is 0, then the thread is alive. If the return value is ESRCH, then
the thread is not alive in the system.

If you use NPTL (linux 2.6 threads), then you can use kill on the thread
(pid would be what got from gettid() system call) with signal 0. If the
return value is 0, then the thread is alive and if it is not zero, then
the thread is not alive.

AFAIK, both of them works fine in my application because, I have my
application running on both linux 2.4 and linux 2.6.

PS: To know if you are using NPTL, issue the command 'getconf
GNU_LIBPTHREAD_VERSION'. If you are using NPTL, the output should be
something like NPTL 2.3.6. If you are using the old
threading(LinuxThreads), the output should be something like
linuxthreads-0.10

HTH.

-- 
Praveen Kumar
http://journal.praveen.ws


More information about the ilugc mailing list