【问题标题】:Perl Threads::QueuePerl 线程::队列
【发布时间】:2016-04-29 00:14:30
【问题描述】:

我正在创建一个 Thread::Queue 元素数组。 我这样做是这样的:

for (my $i=0; $i < $queues_amount; $i++){
    $queues[i]=Thread::Queue->new;
}

但是,当我用这样的元素填充每个队列时

$queues[$index]->enqueue($element);

我收到以下错误:

无法在未定义的值上调用方法“入队”...

您能帮我找出问题所在吗?

我的 perl 版本是 5.12.2。

【问题讨论】:

    标签: multithreading perl


    【解决方案1】:

    您没有在循环中的$i 变量上使用$ sigil:

    for (my $i=0; $i < $queues_amount; $i++){
        $queues[i]=Thread::Queue->new;  # should be $i
    }
    

    如果您启用了use strictsubs 部分将引发关于裸字i 的错误。要捕获许多错误,请将use warnings; use strict; 放在所有程序的顶部。

    写成这样也比较惯用:

    my @queues = map {Thread::Queue->new} 0 .. $queues_amount - 1;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 2015-02-07
      • 1970-01-01
      • 2011-09-27
      • 2012-11-05
      • 2014-07-04
      相关资源
      最近更新 更多