为什么这会打印 12 次? [英] Why does this print 12 times?

查看:35
本文介绍了为什么这会打印 12 次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Perl 的多线程.我的代码:

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);

我想在 $threads 线程中打印 test $howmany 次这个词.此代码打印 test 12 次.问题出在哪里?

I want to print the word test $howmany times in $threads threads. This code prints test 12 times. Where is the problem?

推荐答案

那么我想你想要 for (0..$threads-1)for (1..$threads),而不是 for (0..$threads)

Then I think you want for (0..$threads-1) or for (1..$threads), not for (0..$threads)

:-)

这篇关于为什么这会打印 12 次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆