gpt4 book ai didi

multithreading - 强制 perl 程序使用线程使用所有 CPU 容量

转载 作者:行者123 更新时间:2023-12-01 09:57:18 24 4
gpt4 key购买 nike

我有一台配备 CPU Intel Core i3 的笔记本电脑,我想在 Perl 中创建一个简单的程序来使用 100% 的 CPU容量。我已经阅读了有关 Threads 的内容并搜索了有关并行运行它们的信息,但我无法使用 100% 的 CPU

我的代码:

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;
}

但是在运行 CPU 之后使用率大约是 10%。

如何使用 100% 的 CPU?

最佳答案

您需要让线程真正一些事情。例如。使它们从 0 数到一个大数。休眠不占用 CPU 时间。

关于multithreading - 强制 perl 程序使用线程使用所有 CPU 容量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23327877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com