Ah, I see. I read is_joinable() in the code, but I didn't understand its significance. So the while loop polls each thread to discover when a thread is finished processing a file.

With some slight modifications of the op's code, the output shows that the threads do not appear to be waiting for each other (the code does get stuck in an infinite loop when the threads are done processing the files):

use strict; use warnings; use 5.010; use threads; use threads::shared; my @file_list = ( 'file1', 'file2', 'file3', 'file4', 'file5', ); my $nofiles = @file_list; #number of files my $currfile = 1; #current number of file to process my %MSG : shared; #shared hash my $thr0 = threads->new(\&process, shift(@file_list), 'thr0'); $currfile++; my $thr1 = threads->new(\&process, shift(@file_list), 'thr1'); $currfile++; while(1) { if ($thr0->is_joinable()) { $thr0->join; #check if there are files left to process if($currfile <= $nofiles){ $thr0 = threads->new(\&process, shift(@file_list), 'thr0'); $currfile++; } } if ($thr1->is_joinable()) { $thr1->join; #check if there are files left to process if($currfile <= $nofiles){ $thr1 = threads->new(\&process, shift(@file_list), 'thr1'); $currfile++; } } } sub process{ my($file, $thr_name) = @_; print "$thr_name opening $currfile of $nofiles\n"; #do some stuff sleep int(rand 5); print "$thr_name done with $currfile of $nofiles\n"; } --output:-- thr0 opening 1 of 5 thr1 opening 2 of 5 thr1 done with 2 of 5 thr1 opening 3 of 5 thr1 done with 3 of 5 thr1 opening 4 of 5 thr0 done with 1 of 5 thr0 opening 5 of 5 thr1 done with 4 of 5 thr0 done with 5 of 5

Ugh. That is really icky code.


In reply to Re^3: wait for threads to end in parallel by 7stud
in thread wait for threads to end in parallel by pmarcoen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.