One must sit in cond_wait() while the other is working.

Why? That's complete nonsense.

Both threads can be neither holding the lock, nor waiting for a lock; they can just be doing stuff that doesn't involve using the shared resource.

The most basic principle of locking is that you hold lock for a short a period as possible. Ie. They should only attempt to acquire the lock immediately before they are ready to modify the shared resource.

That's Locking & Syncing 101. That way, if the other thread isn't holding the lock, the attempt to acquire it succeeds immediately, and they can get straight on to making the modification and releasing the lock.

With luck, the other thread was busy with its other stuff for the entire time and was neither affected (slowed down) by the action of the first thread, nor imposed any delay upon the first thread.

Ditto vice versa. When the second thread wants to make its change, if the first thread isn't holding the lock, then it can get straight on with its change, without ever affecting or being affected by the other thread.

And that's the Holy Grail of resource sharing; free running (non-blocking) shared resource access for both (all) threads most if the time, with blocking only occurring: a) occasionally; b) for very short periods; when the dynamic periodicities of the threads happen to coincide.

What you are suggesting -- is what Ikegami's code does -- is to make it so that only one thread can ever be doing *anything* at any given time. And that stupid. You -- quite literally -- would be better off using a single thread and just interleaving the code from the two threads.

It would achieve the same processing, with better performance; and the added bonus that you'd be guaranteed not to create deadlocks.

The purpose of locking is not to ensure that only one thread can run at a time; it is to ensure that only one thread can access the shared resource at any given time.

Which makes what you are suggesting -- and what Ikegami implemented -- broken. By design!


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". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^13: Threads sharing global variable (disingenous) by BrowserUk
in thread Threads sharing global variable by Anonymous Monk

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.