2

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

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 版本太旧(并且安装在我无法控制的系统上)并且不支持要返回的 hash 和 hash-ref 类型通过队列(根据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;
}
4

2 回答 2

4

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

于 2013-07-15T19:28:57.777 回答
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)

}
于 2014-12-10T17:07:43.707 回答