Perl Queue and Threads abnormal exit
I am quite new to Perl, especially Perl Threads. I want to accomplish:
Have 5 threads that will en-queue data(Random numbers) into a Thread::queue
Have 3 threads that will de-queue data from the Thread::queue.
The complete code that I wrote in order to achieve above mission:
#!/usr/bin/perl -w
use strict;
use threads;
use Thread::Queue;
my $queue = new Thread::Queue();
our @Enquing_threads;
our @Dequeuing_threads;
sub buildQueue
{
my $TotalEntry=1000;
while($TotalEntry-- >0)
{
my $query = rand(10000);
$queue->enqueue($query);
print "Enque thread with TID " .threads->tid . " got $query,";
print "Queue Size: " . $queue->pending . "\n";
}
}
sub process_Queue
{
my $query;
while ($query = $queue->dequeue)
{
print "Dequeu thread with TID " .threads->tid . " got $query\n";
}
}
push @Enquing_threads,threads->create(\&buildQueue) for 1..5;
push @Dequeuing_threads,threads->create(\&process_Queue) for 1..3;
Issues that I am Facing:
The threads are not running as concurrently as expected.
The entire program abnormally exit with following console output:
Perl exited with active threads: 8 running and unjoined 0 finished and
unjoined 0 running and detached Enque thread with TID 5 got
6646.13585023883,Queue Size: 595 Enque thread with TID 1 got
3573.84104215917,Queue Size: 595
Any help on code-optimization is appreciated.
No comments:
Post a Comment