简体   繁体   中英

Threading taking up large amounts of CPU

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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