Come ottenere il numero di CPU o di core in Perl. Voglio questo, per decidere, creando il numero di thread in modo dinamico. Di seguito ho creato 3 thread. Ma voglio creare discussioni basate sul numero di core in quella macchina.Numero di CPU/core in Perl
#!/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();
A parte: (1) * Sempre * 'usare rigorosamente; usare avvertenze; '. Il modulo 'warnings' è superiore all'interruttore' -w'. (2) Utilizzare un 'Thread :: Queue' invece di proteggere un array con un semaforo. (3) Non puoi dichiarare un sub come "sub & foo {...}". È 'sub foo {...}'. (4) Il '$ fname' può essere' undef'. Non si tiene conto di ciò quando si stampa e si esegue 'csh'. – amon