It seems to me that there should be a problem--thr1 could be sitting their idle while thr0->join is blocking--but I don't know how you can see that problem in the output.

If it will work for you, you don't need to create new threads. You can just use the same two threads over and over:

use strict; use warnings; use 5.010; use threads; use threads::shared; use Thread::Queue; my $q : shared; $q = Thread::Queue->new; #a Queue is thread safe, which means #threads can read from it without #interfering with each other. my @files = ( 'file1', 'file2', 'file3', 'file4', 'file5', ); my $thread_count = 2; my @threads; for (1 .. $thread_count) { my $thread_name = "thread$_"; push @threads, threads->create(\&do_stuff, $thread_name); } $q->enqueue(@files); #...and the starting gun sounds! for (1 .. $thread_count) { $q->enqueue("END_OF_QUEUE"); } for my $thr (@threads) { $thr->join(); } sub do_stuff { my $thr_name = shift; while ( (my $file = $q->dequeue) ne "END_OF_QUEUE" ) { #$q->dequeue() blocks until there is something to retrieve #from the queue. say "$thr_name is opening file: $file"; #doing some work: sleep int(rand 4); say "$thr_name is is done with: $file"; } } --output:-- thread1 is opening file: file1 thread2 is opening file: file2 thread2 is is done with: file2 thread2 is opening file: file3 thread1 is is done with: file1 thread1 is opening file: file4 thread2 is is done with: file3 thread2 is opening file: file5 thread2 is is done with: file5 thread1 is is done with: file4

In reply to Re: 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.