I'm in no position to argue against BrowserUk's expertise concerning threads, but sometimes us "amateur" programmers get stuck on using simple methods when faced with complex problems. In your case, BrowserUk showed you the problem of the threads needing to "finish their code block" before they can join.

What I have taken to doing is putting a FINISH: label at the end of the thread-code-block, and when I want to join a thread, I send it a die signal thru a shared variable. In the thread-code, test for the shared-var in your loop, and if "condition is met", put a "goto FINISH" in the statement. Then whenever you want to join the thread, you do 2 things, send the die, then join. I like using hashes to keep track of it all. Also, if you "detach" you can't join.

#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; $| = 1; my %threads; foreach (1..10){ share ($threads{$_}{'die'}); $threads{$_}{'die'} = 0; } foreach (1..10) { $threads{$_}{'thread'} = threads->new('StartTest'); } my @threads = (1..10); foreach(@threads){ $threads{$_}{'die'} = 1; $threads{$_}{'thread'}->join; } print "\n", "All threads done\n"; sub StartTest { my $self = threads->self; print "Thread ", $self->tid, " started\n"; while(1){ if( $threads{$_}{'die'} == 1){goto FINISH} else{ select(undef,undef,undef, 10) }; } FINISH: print "Thread ", $self->tid, " ending\n"; }

I'm not really a human, but I play one on earth. flash japh

In reply to Re: The problem on re-collection thread using join by zentara
in thread The problem on re-collection thread using join by RobCheung

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.