Ok.. I have this working code
The problem with it is that if one of the threads gets hung it seems to block on the join and will hold up everything till it clears.
What I am trying to do is keep X number of threads running all the time regardless of how long it takes a single one to complete. I normally use Parallel::Forkmanager which does this very well but it is for forking and not for threads.
I think I can replicate it by using threads::shared and doing some kind of signaling through a shared variable that I can check with out blocking. It might even be possible to just detach the threads and write the results back to shared variable, but I thought I would post here and see if someone else already knows of a good solution.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
![[noevil] [noevil] [noevil]](/data/assets/smilies/noevil.gif)
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
Code:
use Threads;
$process_this_many = "100";
$max_threads = "20";
$worker = 0;
while ($worker <= $process_this_many) {
for ($x = 1; $x <= $max_threads && $worker <= $process_this_many; $x++) {
$thr = threads->new(\&sub1, "$x", "$worker");
print "Start $x $worker\n";
$worker++;
}
# Loop through all the threads
foreach $thr (threads->list) {
# Don't join the main thread or ourselves
if ($thr->tid && !threads::equal($thr, threads->self)) {
$data = $thr->join;
print "$data\n";
}
}
}
sub sub1 {
$in = shift @_;
$in2 = shift @_;
sleep 1;
return "Finish $in $in2";
}
The problem with it is that if one of the threads gets hung it seems to block on the join and will hold up everything till it clears.
What I am trying to do is keep X number of threads running all the time regardless of how long it takes a single one to complete. I normally use Parallel::Forkmanager which does this very well but it is for forking and not for threads.
I think I can replicate it by using threads::shared and doing some kind of signaling through a shared variable that I can check with out blocking. It might even be possible to just detach the threads and write the results back to shared variable, but I thought I would post here and see if someone else already knows of a good solution.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
![[noevil] [noevil] [noevil]](/data/assets/smilies/noevil.gif)
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;