I don't think you're understanding the semantics of lock. When you call lock on a locked variable the call blocks until the lock is freed. Every loop iteration is its own lexical scope, so when you fall off the end locks are freed. Your comments say that the other thread won't unlock the $Buffer until after its full, so the last if ($Buffer); to continue on your program is unnecessary unless your semantics differ from what you've told us. To ensure timely lock freeing you can use bare blocks:

#initialize... { lock($Buffer); # We've got the lock # Do stuff } # Fall off the end of our enclosing scope

Also for your queue have you considered using a Thread::Queue if $Buffer is a string buffer this will be easier than dealing with making sure locks are controlled properly. If the filling thread fills it before relinquishing the lock then instead of mucking with the locks push the full buffer into the queue and then the processor of these buffers just dequeues the full buffers and acts on them. The dequeue method simply waits for data, and you can have multiple threads waiting on a Queue so your processing thread (if it doesn't require atomic output or something) can have multiple (number of cores + 1) instances processing.


In reply to Re: need a thread to wait until a buffer is full. by Trizor
in thread need a thread to wait until a buffer is full. by exodist

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.