如何在 Perl 中获取 CPU 或核心数。我希望这个来决定动态创建线程数。下面我创建了 3 个线程。但我想根据该机器中的内核数创建线程。
#!/usr/bin/perl -w
use threads;
use Thread::Semaphore;
my $semaphore = Thread::Semaphore->new();`enter code here`
my $cur_dir = "D:\\qout";
opendir( CURDIR, "$cur_dir" );
my @file_list : shared = readdir(CURDIR);
closedir(CURDIR);
$thr1 = threads->create( \&changemode, \@file_list, "th1" );
$thr2 = threads->create( \&changemode, \@file_list, "th2" );
$thr3 = threads->create( \&changemode, \@file_list, "th3" );
sub &changemode {
my ($file_list) = shift;
my ($message) = shift;
my ($i) = shift;
while (@{$file_list}) {
my $fname;
$semaphore->down();
if (@{$file_list}) {
$fname = shift(@{$file_list});
}
$semaphore->up();
print("$message got access of $fname\n");
system ("csh -fc \"chmod +w $fname\"");
#sleep (2);
}
}
$thr1->join();
$thr2->join();
$thr3->join();