gurjit has asked for the wisdom of the Perl Monks concerning the following question:
The below code is similar to above one excepts use a function to send data and call that function in another thread but it doesn't work:use Thread::Queue; use threads; my $q = Thread::Queue->new(); # A new empty queue print "$q\n"; my $command = "ls -la"; print ("sending command...\n"); $q->enqueue($command); #sleep(60); $command = "pwd"; print ("sending command...\n"); $q->enqueue($command); #sleep(60); $q->end(); # Worker threads my $thr = threads->create(sub { print "running thread : $q\n"; while ((my $item = $q->dequeue())) { print "Running Command : $item \n"; my $result = `$item 2>&1`; print "Result of $item : $result \n"; } } ) ; # terminate. $thr->join();
#!/usr/bin/perl use Thread::Queue; use threads; use threads::shared; my $q ; share($q); $q = Thread::Queue->new(); my $thr1 = threads->create(sub { my ($q) = @_; print "Running Thread1 : $q\n"; my $command = "ls -la"; print ("sending command...\n"); $q->enqueue($command); #sleep(60); $command = "pwd"; print ("sending command...\n"); $q->enqueue($command); #sleep(60); $q->end(); } , $q) ; # Worker threads my $thr2 = threads->create(sub { my ($q) = @_; print "running thread2 : $q\n"; while ((my $item = $q->dequeue())) { print "Running Command : $item \n"; my $result = `$item 2>&1`; print "Result of $item : $result \n"; } } ,$q) ; $thr1->join(); $thr2->join(); print "Done\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl thread issue
by huck (Prior) on Dec 29, 2016 at 00:05 UTC | |
by gurjit (Initiate) on Dec 29, 2016 at 00:10 UTC | |
by haukex (Archbishop) on Dec 29, 2016 at 11:15 UTC | |
|
Re: Perl thread issue
by BrowserUk (Patriarch) on Dec 29, 2016 at 04:21 UTC | |
|
Re: Perl thread issue
by gurjit (Initiate) on Dec 28, 2016 at 23:54 UTC | |
by Anonymous Monk on Dec 29, 2016 at 00:14 UTC | |
by gurjit (Initiate) on Dec 29, 2016 at 00:27 UTC | |
by Anonymous Monk on Dec 29, 2016 at 04:04 UTC |