multithreading - Not able to push into array from subroutine in perl -
i using multithread , want push local time of each thread array. can print localtime thread successfully, wont push time array. printing array not giving blank.
please check.
my code:
#!/usr/bin/perl use threads; use www::mechanize; use lwp::useragent; @arr=(); $num_of_threads = 2; @threads = initthreads(); foreach(@threads){ $_ = threads->create(\&dooperation); } foreach(@threads){ $_->join(); } foreach(@arr){ print "$_\n"; } sub initthreads{ @initthreads; for(my $i = 1;$i<=$num_of_threads;$i++){ push(@initthreads,$i); } return @initthreads; } sub dooperation{ ##doing main operation here $a=localtime(); print "$a\n"; push(@arr,$a); }
threads don't share variables. see threads::shared.
use threads; use threads::shared; @arr :shared;
Comments
Post a Comment