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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.