dvinay has asked for the wisdom of the Perl Monks concerning the following question:
use threads; my @arr = (1,2,3,4); my $outnumber=1; print "\n variable outside thread that is in main program $outnumber\n +"; my @threads; foreach (@arr) { push @threads, threads->new(\&doSomething, $_, $outnumber); } foreach (@threads) { $_->join(); } sub doSomething () { my ($thread)=@_; lock $outnumber; my $intervariable=$outnumber; print "\n Before increment is $intervariable\n"; ++$intervariable; print "\n After increment is $intervariable\n"; print "thread $thread\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Multi-threaded script
by roboticus (Chancellor) on Sep 13, 2013 at 05:05 UTC | |
by dvinay (Acolyte) on Sep 13, 2013 at 06:16 UTC | |
by Random_Walk (Prior) on Sep 13, 2013 at 06:47 UTC |