So I'm wondering in WHAT WAY is a queue safer than a shared variable?

Ostensibly, in no way. Given that a queue is just a shared array under the covers.

But, accessing shared variables requires locking. With Thread::Queue that locking is taken care of for you. It has been used many times by many people and thus proven correct.

There is certainly nothing stopping you from using a shared hash and performing the locking yourself. But then you have the responsibility to test that you're doing it correctly. Whilst not exactly rocket science, it is still possible to get it wrong.

From a practical stand point -- based upon the little you've said about it -- it seems that at some regular interval, your GUI (re-)draws the on-screen representation of the hash by iterating over its keys & values.

Assuming that's how you are doing things, there are several possibilities:

And don't forget that each time you are iterating the hash in the GUI, you will have to lock it to prevent the worker from changing it whilst you are in the process of iterating it. And that means that the worker either has to stop doing what it is meant to be doing and wait for you to unlock the hash before it can record changes -- thus potentially missing something. Or it has to record changes that occur whilst the shared hash is locked, locally, so that it can remain responsive to whatever is causing those changes, and still update the hash properly once it becomes unlocked.

Now I have no idea how time critical your application is, so maybe none of that is a concern for you.

But imagine how much simpler it becomes if instead of the worker locking & updating a shared hash and then unlocking it again; and the GUI constantly locking the hash and iterating over it to discover what changed, if the worker simply told the GUI what changed when it changed. And the GUI does nothing until it receives that notification.

And the easy way to do that is a queue.

When something changes, instead of setting the key/value into the shared hash, it simply enqueues a message of the form: "key:value".

And when the GUI makes it regular check, it just checks if there is anything pending in the queue. If not, it does nothing. If there is, it pulls the message and either updates the screen directly or updates a local hash and then invokes a redraw.

No waiting; no locking (except under the covers of Thread::Queue); no iterating a hash to discover changes; no make-work. When something changes, you get told not just that something has changed, but what.

It takes far less time/cpu to push and pop a single value to a shared array than it does to iterate a shared hash looking for changes, so there is less contention also.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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.

The start of some sanity?


In reply to Re^9: bidirectional pipe freezing by BrowserUk
in thread bidirectional pipe freezing by chessgui

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.