in reply to Perl Threads
The problem is that you are creating all your threads; then joining them. Although you are limiting the number of running threads; because you aren't joining them; they are all hanging around in their non-running, joinable state; and that it what is consuming your memory.
Given that you aren't retrieving anything from the threads when you join them, the simple solution is to avoid having to join them, by detaching them at the point of creation.
Instead of this: $active_threads{ @row } = threads->create ( \&deleteDevice, @row );
Do this: threads->create( \&deleteDevice, @row )->detach;
That way the threads will clean themselves up automatically on termination.
However, there is a problem with this; namely that once your main loop creating the threads finished, you can no longer use:
foreach( threads->list() ) { $_->join(); }
to ensure that the main thread doesn't end before the worker threads complete; and unfortunately, the authors of threads didn't provide a mechanism for waiting until all detached threads complete; nor even a way of checking if any detached threads currently exist.
There are several approaches to dealing with this; some more complicated than others and most have their own sets of problems. The simplest way I know is to use my count-them-out-and-count-them-back method:
use threads::shared; ## at the top ... my $running : shared = 0; ## before the main loop ... sleep 1 while $running; ## after the main loop in-place of the join lo +op sub deleteDevice($) { ++$running; .... # as now --$running; }
Add that lot together and your memory problems should go away.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl Threads ( main thread is detached )
by Anonymous Monk on Aug 13, 2015 at 23:40 UTC | |
by BrowserUk (Patriarch) on Aug 13, 2015 at 23:56 UTC | |
|
Re^2: Perl Threads
by Monkless (Acolyte) on Aug 14, 2015 at 17:10 UTC |