I am learning Perl’s multithreading. My code:
use warnings;
use threads;
use threads::shared;
$howmany = 10;
$threads = 5;
$to = int($howmany / $threads);
for (0 .. $threads) {$trl[$_] = threads->create(\&main, $_);}
for (@trl) {$_->join;}
sub main {
for (1 .. $to) {
print "test\n";
}
}
exit(0);
I want to print the word test $howmany times in $threads threads.
This code prints test 12 times. Where is the problem?
Then I think you want
for (0..$threads-1)orfor (1..$threads), notfor (0..$threads)🙂