thread library--how to check on health of a thread?

thread library--how to check on health of a thread?

Post by Jay G. Sco » Fri, 24 Jun 2005 03:33:36



greetings,

we shall assume posix threads for the sake of the argument.
how does one find out if a thread is still running?
i don't want to do a join, because that will make it stop,
and i don't want it to stop if it's not done.
this seems like an obvious thing to do, yet i can't find it,
and the guy i work with can't find it.

j.
--

Head of Sun Support, Sr. Operating Systems Specialist
Applied Research Labs, Computer Science Div.                   S224
University of Texas at Austin

 
 
 

thread library--how to check on health of a thread?

Post by Frank Cusac » Fri, 24 Jun 2005 04:26:29



Quote:> greetings,

> we shall assume posix threads for the sake of the argument.
> how does one find out if a thread is still running?
> i don't want to do a join, because that will make it stop,
> and i don't want it to stop if it's not done.

No, join waits until the thread terminates and collects it's status.
It's like wait().  Unless you meant, the calling thread stops, which
is correct.

Quote:> this seems like an obvious thing to do, yet i can't find it,
> and the guy i work with can't find it.

There's no way to do this directly, since there's no waitpid()
equivalent for threads (ie, no non-blocking join).

You could use pthread_kill(thread, 0).

There are other ways as well.  You could use a semaphore.  Or simply a
thread_running[] array (set it when the thread starts, clear it when
the thread exits).

Frank

 
 
 

thread library--how to check on health of a thread?

Post by roger.faulk.. » Fri, 24 Jun 2005 05:18:32



> greetings,

> we shall assume posix threads for the sake of the argument.
> how does one find out if a thread is still running?
> i don't want to do a join, because that will make it stop,
> and i don't want it to stop if it's not done.

No it doesn't.  pthread_join() makes the caller wait
until the target thread has terminated.  It doesn't do
anything to the target thread.

Quote:> this seems like an obvious thing to do, yet i can't find it,
> and the guy i work with can't find it.

> j.

If all you want to know is whether the thread is still alive,
you can use pthread_kill() to send signal 0 :
    if (pthread_kill(tid, 0) == ESRCH)
        thread doesn't exist;
    else
        thread is still alive;

This assumes that 'tid' is still valid.  It might not be still
valid if the thread was a detached thread (the tid may have been
reused for a new thread if the old thread exited), or if some
thread has already done a pthread_join() on it, so do this
only on threads that you know are not detached threads that have
not been joined yet.

Roger Faulkner
Sun Microsystems

 
 
 

thread library--how to check on health of a thread?

Post by dick.dun.. » Mon, 27 Jun 2005 06:23:31


How about using dtrace pid provider, and watch for tid activity.
Solaris 10, of course  (you didn't say).
 
 
 

1. Threads, threads, threads

Well, I solved some of my problems with X11R6 & Solaris 2 threads, having
belatedly discovering XInitThreads() & the related lock/unlock functions.
However I still cannot get XNextEvent() to work correctly. I have 2 threads,
one blocks on a semaphore & the other on XNextEvent(). When the data
thread is done the XNextEvent() thread should kick in if an event is
waiting, but it doesn't do so consistently. I've been reduced to spinning
on XPending(), which largely defeats the purpose of the multithreading. Is
there a reason that XNextEvent() isn't thread safe? From what I can make
out from the source it will ultimately use either select() or poll() to
block & both are supposed to be thread safe in Solaris 2.

A second problem seems to be that thr_yield() doesn't seem to actually do
anything. If the data thread yields when done the XNextEvent() (or the
kludged XPending()) thread doesn't get the processor, even when it is at a
higher priority. I'm reduced to suspending the data thread so that the
other can check for user input & then restart it if there is none. What
could be happening that prevents the yield from allowing the other thread
to start?

--

National Center For Atmospheric Research        lokkar borni under sole-vegg
Box 3000 Boulder, CO 80307-3000                 Gj?'i med sitt shinn
303-497-2057                                    jagar borni inn.

2. comp.unix.sco Administrative FAQ

3. Threads in linux versus threads in NT and threads in Solaris.

4. gethostent() and getnetent()

5. how to check for completion of all threads when mutiple threads are running

6. FIPS for linux partition?

7. Threads in linux versus threads in NT and threads in Solaris.

8. ultra 10 doesn't boot

9. threads packages: kernel threads vs. user threads

10. POSIX threads, thread-specific data: what about the "main" thread?

11. Runaway Linux processes-Native Posix Threading Library- Old linux threads

12. does linux JDK 1.1.x use posix threads (pthreads) or green threads?

13. Differences between Solaris threads and POSIX threads