By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,903 Members | 1,081 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,903 IT Pros & Developers. It's quick & easy.

Issue with Thread:Que

gpraghuram
Expert 100+
P: 1,275
Hi,
I am trying to use Perl Thread:Queue module.
I have written this piece of code

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. use Thread;
  4. use Thread::Queue;
  5.  
  6. my (@Que,@ThreadQ);
  7. my $MAX_THREADS    = 1;
  8.  
  9.  
  10. for my $i(0 .. $MAX_THREADS){
  11.     $Que[$i] = Thread::Queue->new();
  12.     push @ThreadQ, threads->new(\&ThreadFun,$i);
  13. }
  14.  
  15. for my $i(0 .. $MAX_THREADS){
  16.     print "Pushing \"THREADVAL:\"$i to thread :$i\n";
  17.     $Que[$i]->enqueue("THREADVAL:" . "$i");
  18. }
  19.  
  20. sleep(20);
  21.  
  22.  
  23. for(@ThreadQ)
  24. {
  25.     $_->join;
  26. }
  27. @ThreadQ = ();
  28.  
  29.  
  30. sub ThreadFun()
  31. {
  32.     my $QId     = $_[0];
  33.     my $IPArgs     = "";
  34.     print "In thread:$QId\n";
  35.     while ($IPArgs = $Que[$QId]->dequeue) 
  36.     #while ($IPArgs = $Que[$QId]->dequeue) 
  37.     {
  38.         if($IPArgs)
  39.         {
  40.             print "In Thread:$QId:$IPArgs\n";
  41.         }
  42.         else
  43.         {
  44.             print "Sleeping in thread:$QId\n";
  45.             sleep(1);
  46.         }
  47.     }
  48. }
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
Jan 14 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.