Issue with Thread:Que

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    Issue with Thread:Que

    Hi,
    I am trying to use Perl Thread:Queue module.
    I have written this piece of code

    Code:
    use strict;
    
    use Thread;
    use Thread::Queue;
    
    my (@Que,@ThreadQ);
    my $MAX_THREADS	= 1;
    
    
    for my $i(0 .. $MAX_THREADS){
    	$Que[$i] = Thread::Queue->new();
    	push @ThreadQ, threads->new(\&ThreadFun,$i);
    }
    
    for my $i(0 .. $MAX_THREADS){
    	print "Pushing \"THREADVAL:\"$i to thread :$i\n";
    	$Que[$i]->enqueue("THREADVAL:" . "$i");
    }
    
    sleep(20);
    
    
    for(@ThreadQ)
    {
    	$_->join;
    }
    @ThreadQ = ();
    
    
    sub ThreadFun()
    {
    	my $QId 	= $_[0];
    	my $IPArgs 	= "";
    	print "In thread:$QId\n";
    	while ($IPArgs = $Que[$QId]->dequeue) 
    	#while ($IPArgs = $Que[$QId]->dequeue) 
    	{
    		if($IPArgs)
    		{
    			print "In Thread:$QId:$IPArgs\n";
    		}
    		else
    		{
    			print "Sleeping in thread:$QId\n";
    			sleep(1);
    		}
    	}
    }
    When i use the $Que[$QId]->dequeue call the program dont exit and when i use $Que[$QId]->dequeue_nb the program exits.
    But real problem is the thread is not able to read any of the enqued data.
    can someone help me with this?

    Thanks
    Raghu
Working...