简体   繁体   English

线程占用大量CPU

[英]Threading taking up large amounts of CPU

I'm using Thread to help do threads in perl; 我正在使用Thread帮助在perl中执行线程; I'd say I'm fairly new to threading. 我想说我对线程技术还很陌生。

I have a variable in my program called "max threads". 我的程序中有一个名为“最大线程数”的变量。 If the number of threads falls below this number, it will prompt a new one. 如果线程数低于此数目,它将提示一个新线程。 I'm using a while loop to compare the current number of existing threads to the maximum threads variable. 我正在使用while循环将现有线程的当前数量与最大线程变量进行比较。

I'm assuming that the while loop is the thing consuming my cpu. 我假设while循环是消耗我的CPU的事情。

Is there anyway that I can have the 'boss' or 'manager' thread (The core thread) not take up as much cpu while arranging and managing threads? 无论如何,在安排和管理线程时,我是否可以拥有“老板”或“经理”线程(核心线程)占用的CPU不那么多? If my CPU is raising just because of the manager thread, then there's ultimately no point to threading at all! 如果我的CPU只是由于管理器线程而上升,那么根本就没有线程可言!

If you want to keep the current model, you should have some kind of signal (probably a semaphore) on which the thread launcher can block when there are too many workers. 如果要保留当前模型,则应该有某种信号(可能是信号量),当有太多工作程序时,线程启动器可以在该信号上阻止该信号。

A much simpler model is to have a pool of workers, and given them work via a Thread::Queue. 一个更简单的模型是拥有一组工作程序,并通过Thread :: Queue给他们工作。

my $q = Thread::Queue->new();

my @workers;
for (1..$MAX_WORKERS) {
    push @workers, async {
       while (my $job = $q->dequeue()) {
           ...
       }
    };
}

for (...) {
    $q->enqueue(...);
}

# Time to exit
$q->enqueue(undef) for 0..$#workers;

# Wait for workers to finish.
$_->join() for @workers;

I don't use Perl, but speaking from a general asynchronous programming perspective, you want a thread pool manager that isn't clogging up the main thread, and this can be accomplished multiple ways. 我不使用Perl,但是从一般的异步编程角度来讲,您想要一个不会阻塞主线程的线程池管理器,并且可以通过多种方式来实现。 For one thing, you can dedicate a thread (yay!) to doing something like this (pseudocode): 一方面,您可以将一个线程(是的!)专用于执行以下操作(伪代码):

while program not terminating:
   wait a quarter-second or so, then
      do your "are-there-enough-threads" check

The OS, or your abstracted run-time library, will generally supply some kind of wait function that halts the thread until a specific amount of time has passed (thus taking up no scheduler resource during that time). 操作系统或抽象的运行时库通常会提供某种等待函数,该函数将线程暂停直到经过特定时间(因此在此期间不占用任何调度程序资源)。

Alternatively, if your program is event-driven (as in a GUI environment), you could do similar pool management off the main thread by posting yourself timer messages, which is another service generally supplied by the OS. 另外,如果您的程序是事件驱动的(例如在GUI环境中),则可以通过发布自己的计时器消息来在主线程外进行类似的池管理,这是操作系统通常提供的另一项服务。

Perl threads are heavy-weight compared to other languages. 与其他语言相比,Perl线程的工作量很大。 They take a lot of resources to start; 他们需要大量资源才能启动; try to start all the threads you need up front and just keep them running. 尝试先启动所有需要的线程,然后保持它们运行。 Starting new threads every time you have an asynchronous task to do will be very inefficient. 每当您有一个异步任务要执行时,启动新线程将效率很低。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM