gpt4 book ai didi

multithreading - Perl中的并发子例程

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

我试图使这两个子例程同时运行。当我启动程序时,我最终得到:

Perl exited with active threads:
2 running and unjoined
0 finished and unjoined
0 running and detached

什么都不会打印到控制台上。我被期望while(1)保持活跃直到SIGTERM。

线
use threads;

my $t1 = threads->create( \&sub2 );
my $t2 = threads->create( \&sub1 );

sub sub2 {
my $dbh = get_connector();
while (1) {
print "Worker 2 Running";
sleep 2;
}
}

sub sub1 {
while (1) {
print "Worker 1 Running";
sleep 1;
}
}

您能在这里发现缺陷吗?

最佳答案

您无需等待 child 结束就可以退出该过程。添加

$_->join for threads->list;

关于multithreading - Perl中的并发子例程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19410319/

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