プロが教える店舗&オフィスのセキュリティ対策術

threads のモジュールを使ってマルチスレッドのテストをすると下のようなエラーが
発生します。
何が原因なんでしょうか?

Usage: threads->create(function, ...) at C:\testPerl\test9\test0006.pl line 11.


use strict;
use threads;
use Data::Dumper;
no strict "refs";

my $countM;
for ($countM = 0; $countM < 5; $countM++){

my $test = "test".$countM;
${"thd".$countM}= threads->new(\&mtest($test));

${"thd".$countM}->join;

}

print "test end.\n";


sub mtest {

my $name = @_;
print "$name"."\n";

threads->yield();
}

A 回答 (2件)

2箇所誤りがあります。


#誤の行を#正に直してください。
以下のようになります。
----------------------------------------
use strict;
use threads;
use Data::Dumper;
no strict "refs";

my $countM;
for ($countM = 0; $countM < 5; $countM++){

my $test = "test".$countM;
#${"thd".$countM}= threads->new(\&mtest($test)); #誤
${"thd".$countM}= threads->new(\&mtest,$test); #正

${"thd".$countM}->join;

}

print "test end.\n";


sub mtest {

#my $name = @_; #誤
my $name = shift(@_); #正
print "$name"."\n";

threads->yield();
}
-----------------------------------------
以下、実行結果
test0
test1
test2
test3
test4
test end.
    • good
    • 0

threads->new(\&mtest($test))


って何をしたいんでしょうか?

確認だけど, threads::new の使い方は理解できてるよね?

参考URL:http://perldoc.perl.org/threads.html
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!