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";