stephan_a has asked for the wisdom of the Perl Monks concerning the following question:
Does anybody now how I can call the subs that I pass to a thread? Thanks for your help! Kind regards, Stephanuse strict; use warnings; use threads; use threads::shared; use Thread::Queue; my $queue = new Thread::Queue; sub hello { print "Hello\n"; } sub worker { my $tid = threads->self->tid; print "[$tid] Starting\n"; while (my $job = $queue->dequeue) { print "Got a job\n"; } print "[$tid] Terminating\n"; } my @tlist; my $workers = 4; # Start Workers for (my $i = 0; $i < $workers; $i++) { push(@tlist, threads->new(\&worker)); } for (my $i = 0; $i < 10; $i++) { $queue->enqueue(\&hello); # This causes my trouble } sleep(5); # Wait for workers to finish foreach my $w (@tlist) { $queue->enqueue(undef); } foreach my $w (@tlist) { $w->join; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Threaded Perl: References to subroutines to threads
by liverpole (Monsignor) on Oct 09, 2007 at 14:19 UTC | |
|
Re: Threaded Perl: References to subroutines to threads
by renodino (Curate) on Oct 09, 2007 at 15:21 UTC | |
by Anonymous Monk on Oct 09, 2007 at 15:53 UTC | |
|
Re: Threaded Perl: References to subroutines to threads
by BrowserUk (Patriarch) on Oct 09, 2007 at 23:16 UTC |