我正在学习如何在 Perl 中进行线程化。我在这里查看了示例代码并稍微修改了解决方案代码:
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use Thread::Semaphore;
my $sem = Thread::Semaphore->new(2); # max 2 threads
my @names = ("Kaku", "Tyson", "Dawkins", "Hawking", "Goswami", "Nye");
my @threads = map {
# request a thread slot, waiting if none are available:
foreach my $whiz (@names) {
$sem->down;
threads->create(\&mySubName, $whiz);
}
} @names;
sub mySubName {
return "Hello Dr. " . $_[0] . "\n";
# release slot:
$sem->up;
}
foreach my $t (@threads) {
my $hello = $t->join();
print "$hello";
}
当然,现在这完全被打破了,并且不起作用。它导致此错误:
C:\scripts\perl\sandbox>threaded.pl
Can't call method "join" without a package or object reference at C:\scripts\perl\sandbox\threaded.pl line 24.
Perl exited with active threads:
0 running and unjoined
9 finished and unjoined
0 running and detached
我的目标有两个:
- 强制在任何给定时间允许的最大线程数
- 为要使用的线程提供“工作”数组
在最初的解决方案中,我注意到0..100;
代码似乎指定了分配给线程的“工作量”。但是,在我想提供一系列工作的情况下,我还需要提供类似的东西吗?
非常欢迎任何指导和更正。