As brother poolpi has indicated, your code is an ugly mish mosh of threads and process forking...

...assuming mean threads then the code can be reduced somewhat:

use strict ; use warnings ; use threads ; my @array = qw(a b c d e f g h); my @children; my @results; for (1..5) { for (1..2) { push @children, threads->create(\&sub_thread, @array) ; } ; while (@children) { my $child = shift(@children) ; push @results, $child->join() ; } ; } ; sub sub_thread { my (@temp) = (@_) ; my $tid = threads->tid() ; print "child $tid: @temp\n" ; sleep (2) ; return 'whatever' ; } ;
where:

If you want to have two threads on the go at the same time at all times, then you need a mechanism for each thread to signal to the parent that it has finished. One way of doing that is a queue. For example:

use strict ; use warnings ; use threads ; use Thread::Queue ; my @array = qw(a b c d e f g h); my @results; my $finished_queue = new Thread::Queue ; my $c = 2 ; # Number active at once my $n = 10 ; # Total number to dispatch my $m = $n ; # Number to collect while ($m) { while ($n && $c) { my $child = threads->create(\&sub_thread, @array) ; $n-- ; $c-- ; } ; my $tid = $finished_queue->dequeue() ; my $child = threads->object($tid) ; push @results, $child->join() ; $m-- ; $c++ ; } ; sub sub_thread { my (@temp) = (@_) ; my $tid = threads->tid() ; print "child $tid: @temp\n" ; sleep(rand(2)+2) ; $finished_queue->enqueue($tid) ; print "child $tid: finished\n" ; return "whatever from child $tid" ; } ;
noting: (a) that the order of the results is not necessarily the same as the order of dispatch (which requires some extra information related to the $tid); and (b) that even though it doesn't need the $child while it is creating each one, it does need to set scalar context !

Finally, the other way to run two threads is to start two and pass "parameters" and results back and forth using two queues... along the lines of:

use strict ; use warnings ; use threads ; use Thread::Queue ; my @array = qw(a b c d e f g h); my @results; my $param_queue = new Thread::Queue ; my $result_queue = new Thread::Queue ; my $c = 2 ; # concurrent threads my $n = 10 ; # amount of work my @children ; for (1..$c) { # Start the threads push @children, threads->create(\&sub_thread) ; } ; for (1..$n) { # Dispatch the work $param_queue->enqueue([@array]) ; } ; for (1..$n) { # Collect the results push @results, $result_queue->dequeue() ; } ; $param_queue->enqueue((undef) x $c) ; # Signal the threads to stop foreach (@children) { # Collect the terminated threads $_->join() ; } ; sub sub_thread { my $task = 0 ; my $tid = threads->tid() ; print "child $tid: started" ; while (my $p = $param_queue->dequeue()) { $task++ ; my @temp = @$p ; print "child $tid/$task: @temp\n" ; sleep(rand(2)+2) ; $result_queue->enqueue("whatever from child $tid/$task") ; print "child $tid/$task: done\n" ; } ; print "child $tid: terminated\n" ; } ;
which I hope is reasonably self explanatory.


In reply to Re: Perl thread/semaphore by gone2015
in thread Perl thread/semaphore by imrags

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.