gpt4 book ai didi

windows - 为什么我的 ActivePerl 程序报告 'Sorry. Ran out of threads'?

转载 作者:可可西里 更新时间:2023-11-01 10:09:54 29 4
gpt4 key购买 nike

Tom Christiansen's example code (à la perlthrtut )是一个递归的线程实现,用于查找和打印 3 到 1000 之间的所有质数。

下面是稍微改编的脚本版本

#!/usr/bin/perl
# adapted from prime-pthread, courtesy of Tom Christiansen

use strict;
use warnings;
use threads;
use Thread::Queue;

sub check_prime {
my ($upstream,$cur_prime) = @_;
my $child;
my $downstream = Thread::Queue->new;

while (my $num = $upstream->dequeue) {
next unless ($num % $cur_prime);

if ($child) {

$downstream->enqueue($num);

} else {

$child = threads->create(\&check_prime, $downstream, $num);

if ($child) {

print "This is thread ",$child->tid,". Found prime: $num\n";

} else {

warn "Sorry. Ran out of threads.\n";
last;
}
}
}

if ($child) {
$downstream->enqueue(undef);
$child->join;
}
}

my $stream = Thread::Queue->new(3..shift,undef);
check_prime($stream,2);

在我的机器上运行时(在 ActiveState 和 Win32 下),代码在以 'Sorry.线程用完”警告。

在试图弄清楚为什么我受限于我可以创建的线程数量时,我将 use threads; 行替换为 use threads (stack_size => 1);。生成的代码愉快地处理了 2000 多个线程。

谁能解释这种行为?

最佳答案

来自threads documentation :

The default per-thread stack size for different platforms varies significantly, and is almost always far more than is needed for most applications. On Win32, Perl's makefile explicitly sets the default stack to 16 MB; on most other platforms, the system default is used, which again may be much larger than is needed.
By tuning the stack size to more accurately reflect your application's needs, you may significantly reduce your application's memory usage, and increase the number of simultaneously running threads.
Note that on Windows, address space allocation granularity is 64 KB, therefore, setting the stack smaller than that on Win32 Perl will not save any more memory.

关于windows - 为什么我的 ActivePerl 程序报告 'Sorry. Ran out of threads'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2442436/

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