I have another method which processes items in the queue and then sleeps 3 seconds. The problem (I'm assuming) is that sometines the inotifywait method won't release the lock on the queue.

What is the purpose of the 3 second sleep?

It's much more likely that the problem is that your 3 second sleep is preventing the read thread from processing its end of the queue in a timely manner. You can write a huge amount of data to a queue in 3 seconds.

There should be no reason to have sleeps in the reader end of the queue. If you code it so that it just blocks on the dequeue(), it won't consume any cpu until there is something for it to do, but will be able to respond immediately (subject to getting its next timeslice) as soon as there is something to do.

If you are using dequeue_nb() in order to allow that thread to also check for other conditions, for example a "terminate now" command, and you want to prevent that thread running away with the cpu, then you should use a much shorter sleep. A sleep of <timeslice> milliseconds is theoretically most efficient, but any reasonable value will work well. Generally, start with say 50 milliseconds (usleep 50;) and vary it it up or down to strike a balance between idle thread cpu consumption and responsiveness.

As a safe guard to prevent the writing thread running away with memory, it can relinquish its timeslice if the size of the queue gets bigger than some limit: sleep 0 if $Q->pending > 100;. Again, if that thread is not blocking on pipe read, then using a timeslice-sized sleep instead of zero will prevent the thread running away with the cpu.

It would be easier to make suggestions if you posted the reader thread code also.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: Thread::Queue locking question by BrowserUk
in thread Thread::Queue locking question by mhearse

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.