gpt4 book ai didi

multithreading - 在Perl中使用共享哈希

转载 作者:行者123 更新时间:2023-12-03 12:52:15 28 4
gpt4 key购买 nike

为什么会出现此错误?

Thread 1 terminated abnormally: Invalid value for shared scalar at thr_hash.pl line 8.


use threads;
use threads::shared;
use Data::Dumper;

my %h:shared;

threads->create(sub{
$h{manager} = {
name => 'John',
surname => 'Doe',
age => 27
};
})->detach;

sleep 1;

print Dumper \%h;

最佳答案

在分配中使用变量(在这种情况下为匿名哈希)时,请使用shared_clone():

use threads;
use threads::shared;
use Data::Dumper;

my %h:shared;

threads->create(sub{
$h{manager} = shared_clone({
name => 'John',
surname => 'Doe',
age => 27
});
})->detach;

sleep 1;

print Dumper \%h;

输出:
$VAR1 = {
'manager' => {
'surname' => 'Doe',
'name' => 'John',
'age' => 27
}
};

关于multithreading - 在Perl中使用共享哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30584406/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com