开发者

Threading taking up large amounts of CPU

开发者 https://www.devze.com 2023-02-14 21:37 出处:网络
I\'m using Thread to help do threads in 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

I'm using Thread to help do threads in 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.

I'm assuming that the while loop is the thing consuming my 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? If my CPU is raising just because of the manager thread, then there's ultimately no point to threading at all!


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.

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. 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.


Perl threads are heavy-weight compared to other languages. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号