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.