Multithreading Perl线程::用于嵌套数据结构的共享线程
现在,我必须像上面那样,为每个子级别的散列显式地创建一个散列引用 有更好的方法吗 另外,如果我没有创建散列引用,那么我会得到一个“共享标量的无效值”错误,因为我试图将其用作散列。自动激活会导致Multithreading Perl线程::用于嵌套数据结构的共享线程,multithreading,perl,hash,Multithreading,Perl,Hash,现在,我必须像上面那样,为每个子级别的散列显式地创建一个散列引用 有更好的方法吗 另外,如果我没有创建散列引用,那么我会得到一个“共享标量的无效值”错误,因为我试图将其用作散列。自动激活会导致 # thread::shared only allws a single level of shared structure # needs a explicit hash creation for nested data structures my %counts : shared = ();
# thread::shared only allws a single level of shared structure
# needs a explicit hash creation for nested data structures
my %counts : shared = ();
foreach my $state (%known_states) {
# COUNTS
unless (exists $counts{build}) {
my %cb : shared;
$counts{build} = \%cb;
}
$counts{build}{$state} = 0;
}
表现得像
$counts{build}{$state} = 0;
为了便于阅读,我们把它改为两行
( $counts{build} //= {} )->{$state} = 0;
但就像你说的,我们需要一个共享哈希
$counts{build} //= {};
$counts{build}{$state} = 0;
可以添加以下内容以确保不会意外激活非共享变量:
$counts{build} //= &share({});
$counts{build}{$state} = 0;
no autovivification qw( fetch store exists delete );