cicbaba has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I need help in simple threaded solution.
How can I join the active thread?

After I was started the thread I want itto join itself
at the end and go back to the main
Example:

# main program $thr1=new threads \&sub_name; $thr2=new threads \&sub_name; $thr3=new threads \&sub_name; $thr4=new threads \&sub_name; # threaded subroutine sub sub_name{ print ("\nIn the thread!\n"); # here I need help to join this thread after the print }

I need that every started therad will be killed
after finished and thread->list to be updated

Thank you all,
Igor
Please email me to igor.ryaboy@starcore-dsp.com

Replies are listed 'Best First'.
Re: Threads in perl
by BrowserUk (Patriarch) on Oct 08, 2003 at 10:38 UTC

    Why do you want to do this? I ask because I think you may be confused about what join() does.

    The purpose of join is to allow the thread that creates the child thread to retrieve any return value(s) from the child thread. There is no way to do that from the executing thread as, by definition, the executing thread hasn't yet returned, so there is nothing to retrieve. If you do try it, it will simply block forever. It should probably report an error if the attempt is made, but that doesn't happen.

    I think that what you are trying to do is arrange that the threads "go away" automatically when they are finished. To do this, use the detach() method. You can do this either in the main program using the handle returned from the threads creation, or you can do it within the sub by obtaining a handle to the executing thread using the threads->self() method.

    sub subname{ # Let the system know that noone is interested # in the return value from the thread threads->self->detach(); # Do stuff # when the stuff is done return; # Commit suicide in an unmarked grave! }

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail

Re: Threads in perl
by Preceptor (Deacon) on Oct 09, 2003 at 09:00 UTC
    A detached thread (as BrowserUK mentions) will be 'exit' after completion.
    Joining threads is what you do when you are looking to either get a return value from the thread, or you want to confirm that it has finished executing before proceeding through the program. $thread -> join() will block until the thread has completed processing.

    for ( $loop = 0 ; $loop < THREAD_COUNT; $loop++ ) { print "Starting thread $loop.\n"; $my_threads[$loop] = threads -> new ( \&run_sub, $argument1, $argume +nt2 ); print "thread $loop started.\n"; } for ( $loop = 0 ; $loop < THREAD_COUNT; $loop++ ) { print "waiting for thread $loop...\n"; $result += $my_threads[$loop] -> join; print "done.\n"; } print "result was $result.\n"; sub run_sub { my ( $arg1, $arg2 ) = @_; #do stuff return $number; }


    Simple example of how a thread join might work. The program will wait at the 'join' for each thread to complete. The 'main' routine is also able to extract return values from each thread as they exit.

    Extracting data from a thread may be done in this fashion, or by using a shared variable. Be aware that a shared value may be subject to race conditions if not treated carefull.