Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I share a common thread pool amongst objects in Perl?
    primarykey
    data
    text
    <p>I've been trying to extend the first answer at Perl Monks (<a href="http://www.perlmonks.org/?node_id=735923" rel="nofollow">http://www.perlmonks.org/?node_id=735923</a>) to a threaded model to no avail. I keep getting issues with not being able to pass a coderef</p> <p>In my superclass I define the threadpool as a package variable so it can be shared amongst the subclasses:</p> <pre><code>package Things::Generic; my $Qwork = new Thread::Queue; my $Qresults = new Thread::Queue; my @pool = map { threads-&gt;create(\&amp;worker, $Qwork, $Qresults) } 1..$MAX_THREADS; sub worker { my $tid = threads-&gt;tid; my( $Qwork, $Qresults ) = @_; while( my $work = $Qwork-&gt;dequeue ) { my $result = $work-&gt;process_thing(); $Qresults-&gt;enqueue( $result ); } $Qresults-&gt;enqueue( undef ); ## Signal this thread is finished } sub enqueue { my $self = shift; $Qwork-&gt;enqueue($self); } sub new { #Blessing and stuff } . . </code></pre> <p>Now for the subclasses. It is guaranteed that they have a process_thing() method.</p> <pre><code>package Things::SpecificN; use base qw (Things::Generic); sub new() { #instantiate } sub do_things { my $self = shift; #enqueue self into the shared worker pool so that "process_thing" is called $self-&gt;enqueue(); } sub process_thing() { #Do some work here return RESULT; } </code></pre> <p>#</p> <pre><code>Main my @things; push @things, Things::Specific1-&gt;new(); push @things, Things::Specific2-&gt;new(); . . push @things, Things::SpecificN-&gt;new(); #Asynchronously kick off "work" foreach my $thing (@things) { $thing-&gt;do_things(); } </code></pre> <p>My goal is to put a list of "work" on the queue. Each thread will pull work from the queue and execute it, no matter what it. Each Thing has it's own unique work, however the function to do the work will be guaranteed to be called "process_thing". I just want the thread pool to grab an entry from the queue and do the "something". I think I am describing functionality similar to Android AsyncTask.</p> <p>My Perl is not high enough for Thread::Queue::Any</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload