gpt4 book ai didi

multithreading - Perl 线程 - 从模块调用子例程 (pm)

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

启动在额外 perl 模块中定义的子例程的线程的正确语法是什么?

perl 程序:

use strict;
use warnings;
use forks;
require testModule;

# before solution - thanks ysth!
# testModule->main will not work!
#my $thr1 = threads->new(\&testModule->main, "inputA_1", "inputB_1");
#my $thr2 = threads->new(\&testModule->main, "inputA_2", "inputB_2");

# solved
#my $thr1 = threads->new(\&testModule::main, "inputA_1", "inputB_1");
#my $thr2 = threads->new(\&testModule::main, "inputA_2", "inputB_2");
my @output1 = $thr1->join;
my @output2 = $thr2->join;

perl 模块 testModule.pm:
package testModule;
sub main{
my @input = @_;
#some code
return ($output1, $output2)
}

testModule->main 的确切系统调用是什么?

提前致谢!

最佳答案

你几乎是对的:

...threads->new( \&testModule::main, "inputA_1", "inputB_1" );
->仅用于类/实例方法调用;如果您希望将其作为类方法调用(这将使 @input 获得类名以及“inputA_1”和“inputB_1”),那么您可以:
...threads->new( sub { testModule->main(@_) }, "inputA_1", "inputB_1" );

关于multithreading - Perl 线程 - 从模块调用子例程 (pm),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23739370/

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