Multithreading – forces Perl programs to use threads to use all CPU capacity
•
Java
I have a laptop equipped with Intel Core I3 CPU. I want to create a simple program in Perl to use 100% CPU capacity
My code:
use strict;
use warnings;
use threads;
use threads::shared;
print "Starting main program\n";
my $t1 = threads->create(\&sub1,1);
my $t2 = threads->create(\&sub1,2);
my $t3 = threads->create(\&sub1,3);
my $t4 = threads->create(\&sub1,4);
$t1->join();
$t2->join();
$t3->join();
$t4->join();
print "End of main program\n";
sub sub1 {
my $num = 20;
print "started thread $num\n";
sleep $num;
print "done with thread $num\n";
return $num;
}
However, after running, the CPU utilization rate is about 10%
How do I use 100% CPU?
Solution
You need to make the thread really do something For example Make them count from 0 to large Sleeping doesn't take up CPU time
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
