Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 如何在Perl中完成线程清理?_Multithreading_Perl - Fatal编程技术网

Multithreading 如何在Perl中完成线程清理?

Multithreading 如何在Perl中完成线程清理?,multithreading,perl,Multithreading,Perl,我有一个Perl脚本,它在验证某个表达式时启动线程 while ($launcher == 1) { # do something push @threads, threads ->create(\&proxy, $parameters); push @threads, threads ->create(\&ping, $parameters); push @threads, threads ->create(\&dns,

我有一个Perl脚本,它在验证某个表达式时启动线程

while ($launcher == 1) {
    # do something
    push @threads, threads ->create(\&proxy, $parameters);
    push @threads, threads ->create(\&ping, $parameters);
    push @threads, threads ->create(\&dns, $parameters);
    # more threads
    foreach (@threads) {
    $_->join();
    }
}
第一个循环运行正常,但在第二个循环中,脚本退出并出现以下错误:

线程已在launcher.pl行290处连接。 Perl已退出活动线程: 1运行和未连接 0已完成且未连接 0正在运行并已分离


我想我应该清理@threads,但我怎么做呢?我甚至不确定这是否是问题所在。

最简单的解决方案是在while循环中创建数组(
while{my@threads;…}
),除非您在其他地方需要它。否则,您可以在while循环的末尾执行
@threads=()
@threads=undf

您还可以设置一个变量
my$next\u thread在while循环之外,然后在while循环中分配
$next\u thread=@threads
第一件事,并将
foreach
循环更改为

for my $index ($next_thread .. $#threads) {
    $threads[$index]->join();
}
或者跳过这一步,在最后三个添加的线程中循环一个片段

for (@threads[-3..-1) {
    $_->join();
}

只需在循环结束时清除
@threads

@threads = ();
while ($launcher == 1) {
    my @threads;
或者更好的方法是,在循环的开头用
my
声明
@threads

@threads = ();
while ($launcher == 1) {
    my @threads;

这可能不是你唯一的问题,但那肯定是个问题。第一次通过循环加入
@threads[0..2]
。然后你试着加入
@threads[0..5]
,其中有三个线程已经加入了。我想你说得比我优雅。不过,你提到的
@threads
中有一个输入错误。这确实有效!我不知道为什么,但我认为这比笑要困难得多,非常感谢!