Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keeping multiple threads running.

Status
Not open for further replies.

travs69

MIS
Joined
Dec 21, 2006
Messages
1,431
Location
US
Ok.. I have this working code
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]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
You can use "detach" instead of "join". It doesn't work exactly the same way though... once you detach a thread, you can't join it, and when it exits, whatever it returns is just discarded (detach = forget about the thread but let it run as long as it wants to, whereas join = stop and wait for the thread to finish and return something).

Once you detach a thread, shared variables would probably be needed to communicate, since the thread is "forgotten about" now.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
I was thinking about using detach.. the problem is then the threads->list(); doesn't return anything. I was trying to use that to determine how many threads were currently running. I figured I could use shared variables to figure out the status of each thread if it was needed (I normally used a hash in memory to communicate between my forked process normally.. but Forkmanager handles the control of them).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top