cormanaz has asked for the wisdom of the Perl Monks concerning the following question:
However I have a question about the @threads array. Since that exists till then end of the loop, do the thread objects it contains only hold the values returned by the sub, or do they have other data that would eat a lot of memory? If so then the approach I have here won't work for processing all pairs of nodes in a 7000 node graph.#!/usr/bin/perl -w use strict; use threads; use Thread::Semaphore; my $sem = Thread::Semaphore->new(15); # max 15 threads my @threads; for my $i (0..5) { $sem->down; my $t = threads->create(\&mySubName, $i); push(@threads,$t); } foreach my $t (@threads) { print $t->join(),"\n"; } sub mySubName { my $foo = shift(@_); return $foo * 10; # release slot: $sem->up; }
Assuming that's the case I tried this
but the output is 1 0 1 2 3 which is not right.##!/usr/bin/perl -w use strict; use threads; use threads::shared; use Thread::Semaphore; my $sem = Thread::Semaphore->new(15); # max 15 threads my @results :shared = 1; for my $i (0..5) { $sem->down; my $t = threads->create(\&mySubName, $i); } print join(" ",@results); sub mySubName { threads->detach(); my $foo = shift(@_); push (@results,$foo); $sem->up; }
Guidance appreciated.
|
|---|