Dear Monks,

I'm having some trouble with Thread::Queue::Any. I managed to reduce the problem to the following :

############# code starts here ############# use strict; use threads; use threads::shared; use Thread::Queue::Any; our $| = 1; my $QUEUE : shared; my $TERMINATE : shared = 0; my $THREAD; sub TimeThread { while (!$TERMINATE) { sleep 1; $QUEUE->enqueue(scalar(localtime())); } } $SIG{TERM} = sub { $TERMINATE=1; $THREAD->join; exit; }; $QUEUE=Thread::Queue::Any->new; $THREAD=threads->new(\&TimeThread); while (1) { sleep 1; if ($QUEUE->pending) { ######## This is really weird! ######## my $time = $QUEUE->dequeue; print $time; ### NOT OK #print $QUEUE->dequeue; ### OK. } } ############# code ends here ##################
If I run the code as is, the output reads:

1111 ...

and so on.

Now if I replace line no. 37 with:

print $QUEUE->dequeue;
I get the expected result, namely nicely formatted localtime()s.

What am I doing wrong ?

Thank you,

R.

PS: My environment is : WinXP Home Edition; ActivePerl 5.8.3 Build 809; Thread::Queue::Any 0.06


In reply to Thread::Queue::Any->dequeue trouble by mrd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.