Perl   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Perl中的CPU /内核数大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如何获取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,"th2" );
$thr3 = threads->create( \&changemode,"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();

解决方法

查看CPAN模块,如 @L_197_0@
use Sys::Info;
   use Sys::Info::Constants qw( :device_cpu );
   my $info = Sys::Info->new;
   my $cpu  = $info->device( CPU => %options );

   printf "CPU: %s\n",scalar($cpu->identify)  || 'N/A';
   printf "CPU speed is %s MHz\n",$cpu->speed || 'N/A';
   printf "There are %d CPUs\n",$cpu->count || 1;
   printf "CPU load: %s\n",$cpu->load  || 0;

大佬总结

以上是大佬教程为你收集整理的Perl中的CPU /内核数全部内容,希望文章能够帮你解决Perl中的CPU /内核数所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。