Perl错误:线程无法启动:共享标量的值无效

时间:2013-07-15 19:00:01

标签: multithreading perl

尝试运行测试代码时出现以下错误:

thread failed to start: Invalid value for shared scalar at ./threaded_test.pl line 47.

第47行是: %hoh = hoh(@new_array);

我的观察:

  • 如果我删除第47行和其他引用%hoh的行,则脚本会正常运行
  • 我可以创建一个没有错误的新哈希%new_hash = (itchy => "Scratchy");但是当我尝试从另一个子句(第47行)“返回”哈希时,会导致上面的错误。

不幸的是,我无法使用输入/输出队列,因为我使用的Thread :: Queue版本太旧(并且安装在我无法控制的系统上)并且不支持散列和散列引用类型通过队列返回(根据this)。显然,我的版本只支持通过队列返回的字符串。

有没有办法成功执行此操作:$hash{$string}{"jc"} = \%hoh;

#!/usr/bin/perl
use strict;
use warnings;
use threads;
use Thread::Queue;
use constant NUM_WORKERS => 10;

my @out_array : shared = ();
main();

sub main
{
    my @results = test1();
    foreach my $item (@results) {
        print "item: $item\n";
    }
}

sub test1
{
    my $my_queue = Thread::Queue->new();
    foreach (1..NUM_WORKERS) {
        async {
            while (my $job = $my_queue->dequeue()) {
                test2($job);
            }
        };
    }
    my @sentiments = ("Axe Murderer", "Mauler", "Babyface", "Dragon");
    $my_queue->enqueue(@sentiments);
    $my_queue->enqueue(undef) for 1..NUM_WORKERS;
    $_->join() for threads->list();
    my @return_array = @out_array;
    return @return_array;   
}

sub test2
{
    my $string = $_[0];
    my %hash : shared;
    my @new_array : shared;
    my %new_hash : shared;
    my %hoh : shared;
    @new_array = ("tom", "jerry");
    %new_hash = (itchy => "Scratchy");
    %hoh = hoh(@new_array);
    my %anon : shared;

    $hash{$string} = \%anon;    
    $hash{$string}{"Grenade"} = \@new_array;
    $hash{$string}{"Pipe bomb"} = \%new_hash;
    $hash{$string}{"jc"} = \%hoh;
    push @out_array, \%hash;
    return;
}

sub hoh
{
    my %hoh;
    foreach my $item (@_) {
        $hoh{"jeepers"}{"creepers"} = $item;
    }
    return %hoh;
}

2 个答案:

答案 0 :(得分:3)

问题在于您尝试存储对共享变量中未共享的内容的引用。您需要使用前面提到的share,或者需要序列化数据结构。

答案 1 :(得分:1)

#!/perl/bin/perl

use strict;
use threads;
use threads::shared;

my %hm_n2g:shared = ();

my $row = &share([]);
$hm_n2g{"aa"}=$row;
$row->[0]=1;
$row->[1]=2;
my @arr = @{$hm_n2g{"aa"}};
print @arr[0]." ".@arr[1]."\n";

#If you want to lock the hash in a thread-subroutine
{
lock(%hm_n2g)

}