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

标签 multithreading perl

启动额外 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/

相关文章:

c++ - 在不同线程上时,cv::imshow 不显示 cv::mat 颜色

c++ - std::thread < Unresolved 重载函数类型> 错误

c# - 一个 C# 异常可以在不同线程上多次引发吗?

Perl Linux::Inotify2 - 无法再响应事件

perl - 只有在没有加载要求的情况下,如何运行我的 Perl 脚本?

java - JMeter中使用多线程测试时无法解决 "Uncaught Exception java.lang.OutOfMemoryError: unable to create new native thread"

mysql - 为什么使用 mysql_real_connect 建立的与 MySQL 的单独连接似乎对插入设置了限制?

Perl 严格引用错误

arrays - 如何对 Perl 数组进行分页?

perl - 需要帮助从 Perl 生成 JSON