Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
This example might show you a way. One thing you might be missing is that the thread must reach the end of it's code block, or return, before it can be joined. So I usually use a shared var 'die' to signal a thread to return immediately. That way, if you want to exit without all those warnings, you set the 'die' = 1 for each thread, then join them after they have returned.

In the following example, I join them as they finish, but you can easily hold them in an unjoined condition, until all threads are done. I don't know why you would want that, but it would be easy to do.

#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; $| = 1; my %threads; foreach (1..10){ share $threads{$_}{'die'}; share $threads{$_}{'data'}; $threads{$_}{'die'} = 0; $threads{$_}{'data'} = 0; } foreach (1..10) { $threads{$_}{'thread'} = threads->new('StartTest'); } my @threads = (1..10); while(1){ foreach my $t (@threads){ if($threads{$t}{'data'} > 5){ $threads{$t}{'die'} = 1; $threads{$t}{'thread'}->join; @threads = grep { $_ != $t } @threads; } } if(scalar @threads == 0){last} } print "\n", "All threads done\n"; ########################################################## sub StartTest { my $self = threads->self; print "Thread ", $self->tid, " started\n"; while(1){ if( $threads{$_}{'die'} == 1){return} else{ print "From thread $self->",$threads{$_}{'data'}++,"\n"; sleep 1; }; } print "Thread ", $self->tid, " ending\n"; }

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

In reply to Re: Thread exit errors by zentara
in thread Thread exit errors by JFarr

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-23 17:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found