lotus

previous page: 12.1. (DCE, POSIX, UI) When should I use thread-safe "r" library calls? (Programming)
  
page up: Threads Programming FAQ
  
next page: 14. (DCE, UI, POSIX) After I create a certain number of threads, my program crashes (Programming)

13. (POSIX) How can I perform a join on any thread? (Programming)




Description

This article is from the Threads Programming FAQ, by Bryan O'Sullivan bos@serpentine.com with numerous contributions by others.

13. (POSIX) How can I perform a join on any thread? (Programming)

UI threads allow programmers to join on any thread that happens to
terminate by passing the appropriate argument to thr_join(). This is
not possible under POSIX and, yes, there is a rationale behind the
absence of this feature.

Unix programmers are used to being able to call wait() in such a way
that it will return when "any" process exits, but expecting this to
work for threads can cause confusion for programmers trying to use
threads. The important thing to note here is that Unix processes are
based around a notion of parent and child; this is a notion that is
"not" present in most threads systems. Since threads don't contain
this notion, joining on "any" thread could have the undesirable effect
of having the join return once a completely unrelated thread happened
to exit.

In many (perhaps even most) threaded applications, you do not want to
be able to join with any thread in your process. Consider, for
example, a library call that one of your threads might make, which in
its turn might start a few threads and try to join on them. If another
of your threads, joining on "any" thread, happened to join on one of
the library call's threads, that would lead to incorrect program
behaviour.

If you want to be able to join on any thread so that, for example, you
can keep track of the number of running threads, you can achieve the
same functionality by starting detached threads and having them
decrememnt a (suitably locked, of course) counter as they exit.

 

Continue to:













TOP
previous page: 12.1. (DCE, POSIX, UI) When should I use thread-safe "r" library calls? (Programming)
  
page up: Threads Programming FAQ
  
next page: 14. (DCE, UI, POSIX) After I create a certain number of threads, my program crashes (Programming)