Re: pthread_join



Hello,

can some one tell me when you would need to use pthread_join.

pthread_join() is needed when:

1) You want to make sure that a thread has terminated before proceeding
further, or
2) You want the return value of a terminated thread.

Of course you might want to have both 1) and 2).

By default, threads are created joinable. When a thread terminates,
its thread id, the return value and possibly other resources (like
thread stack) are retained by the Pthreads implementation. Until the
defunct thread has been joined from another thread using
pthread_join().

If you don't need to wait for the thread's completion, and you don't
need the thread's return value, then you might want to detach the
thread. This can be done by using the function pthread_detach(). When
you detach a thread, you tell the Pthreads lib that it can release all
the resources associated to the thread when it terminates.

By the way, if you're creating an important number of joinable threads,
but never join them, then you can run into the situation where you
can't create a new thread because resources are lacking.


i want some practice programs on posix threads.suggest me the site names to
get good understanding of pthreads.

I don't know any good tutorial about Pthreads on the net, but if you're
looking for a good book, then I warmly recommend:
"Programming with Posix Threads" from David Butenhof (ISBN
0-201-63392-2).


HTH,
Loic.

.



Relevant Pages

  • Re: Documentation of multithreading in the LRM
    ... If the system simply took back resources that had been acquired by the thread but not released that could lead to dangling references. ... -The ERROR condition is raised and either there is no ERROR ON-unit or the ERROR ON-unit terminates normally. ... Any ON-units established within the thread are given control before the thread actually terminates. ... The user must ensure that any resources a thread has acquired are released and open files are closed, unless they are needed by other threads that are still active. ...
    (comp.lang.pl1)
  • Re: is such exception handling approach good?
    ... When process terminates, ... opened handles and some other resources. ... proper clean-up code to revert internal objects to some valid ...
    (microsoft.public.vc.language)
  • Re: Using PerfMon to hunt for memory leaks
    ... that there doesn't seem to be any leakage occurring. ... the program frees all of the resources when it terminates because PerfMon stops ... If it keeps going up as you create and destroy more windows, ...
    (microsoft.public.win32.programmer.kernel)
  • Re: possible memory link ???
    ... If you do not either join your threads, detach your threads, or create ... you will leak a little bit of memory each time a thread ... long as the directory scanning threads never terminate, ... terminates, you leak its thread information because you didn't join or ...
    (comp.programming.threads)
  • Re: finalize() not guaranteed to be called -- ever
    ... if you are just looking for a clean solution to free your resources ... > when the application terminates, I think Ingo gave you a good answer. ...
    (comp.lang.java.programmer)