hi all, 

I am trying to implement a thread pool using Thread::Queue. I created a pool of
10 threads. The problem is only one thread is being used, and all jobs are queud
up in a line behind the first. Each job is a subroutine call. How can I create
this to be multi-tasking? ..eg jobs being executed non-blocking as threads being
used as available.

Thanks in advance for any help.
-Jer A.

----my code---

my $wthrsz : shared = 0;
my $psize : shared = 10;
my $thrQ : shared = new Thread::Queue;
my @thr;


sub createthrp {
        ($psize) = @_;
        my $nbr = 0;
        for(1..$psize)
        {
                $thr[$nbr] = threads->create("entrypoint");     
                $nbr++;
        }
        
}


sub entrypoint {        
        for (;;) {
                $payload = $thrQ->dequeue_nb;
                Win32::Sleep(100) and next unless $payload;
                $wthrsz++;
                &$payload;
                $wthrsz--;
       }
}


while(1)
{
#...
$thrQ->enqueue(codehere($param));
#...
}




Quoting "(Chris Wagner)" <[EMAIL PROTECTED]>:

> Creating a thread pool isn't hard.  Here's a rudimentary example that u
> should be able to run with.
> 
> $x = 1;
> share $working;
> $jobs = new Thread::Queue;
> ${"t". ++$x} = thread->create("entrypoint") for (1..10);
> 
> sub entrypoint {
>         for (;;) {
>                 $commanddata = $jobs->dequeue_nb;
>                 sleep 1 and next unless $commanddata;
>                 $working++;
>                 do_something($commanddata);
>                 $working--;
>         }
> }
> 
> 
> Now u have a pool of threads, $t1 to $t10.  $working tells u how many are
> available.  The message queue lets u send work that will be picked up by the
> next available thread.
> 
> At 06:15 PM 12/9/2006 -0800, [EMAIL PROTECTED] wrote:
> >How can I implement simple thread-pooling using perl 5.8 threads?
> >I would need some way of injecting code into the loaded threads, have the
> >ability to reuse threads, and keep track of thread slots available.
> >
> >A simple example would be great. I am not an expert when it comes to
> threads,
> >just so you are aware.
> 
> 
> 
> 
> --
> REMEMBER THE WORLD TRADE CENTER         ---=< WTC 911 >=--
> "...ne cede malis"
> 
> 00000100
> 
> _______________________________________________
> Perl-Win32-Users mailing list
> Perl-Win32-Users@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> 




_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to