multithreading - 为线程设计的哈希,以 YAML 格式转储

标签 multithreading perl

尝试在 YAML 文件中转储具有共享引用的嵌套 HashMap 。 Perl 代码在这里。哈希实际上是线程友好的,

use v5.18;
use YAML::XS;
use threads;
use threads::shared;
use Data::Dumper;

my $href1 = shared_clone({
    anotherRef => shared_clone({})
});

$href1->{anotherRef}{test} =  &share({});

print Dumper $href1;

my $path = "./test.yaml";

open  TAG_YAML, '>', $path;
print TAG_YAML Dump($href1);
close TAG_YAML;


1;

最后,我试图转储流,它显示了如下所述的引用

% ./ref-of-refs.pl ; cat test.yaml
---
anotherRef: HASH(0x21897a0)

是否有任何选项可以将完整哈希转储为 test.yaml 文件中的 YAML 格式。 ?

最佳答案

我的猜测是 XS 魔法 threads::shared使用混淆 YAML::XS::Dump()(因为它还使用 XS 遍历 hashref)。我建议您尝试使用纯 Perl 模块 YAML相反:

use strict;
use warnings;
use YAML;
use threads;
use threads::shared;

my $href1 = shared_clone({
    anotherRef => shared_clone({})
});
$href1->{anotherRef}{test} = shared_clone({});
print Dump($href1);

输出:

---
anotherRef:
  test: {}

关于multithreading - 为线程设计的哈希,以 YAML 格式转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67033350/

相关文章:

algorithm - 跨多个序列的最长公共(public)子串

Perl "Not an ARRAY reference"错误

java - 为什么线程在我的例子中没有被饿死

regex - 在 Perl 中编辑/匹配某个特定匹配项下方的 n 行字符串?

perl - DateTime -> set_time_zone(...) 使用世界时区字符串,用于丢失的城镇

regex - Perl 正则表达式对带或不带引号的项目进行分组并忽略空格

java - 没有wait()和notify()的同步块(synchronized block)/方法notifyAll()方法

c - 服务器似乎在接受时断开连接

c# - "lock(obj) {/* empty */}"对线程可见性的影响

c++ - 通缉 : Elegant solution to race condition