You say I am new to Threads concepts. I will talk about the generic problem using an analogy which I hope is not too simplistic. I don't use curses and I come from a hardware background, but I hope this will make sense.

Conceptually there is only one screen. Typical operations might be move to X, display Y (move, display). Let's say that you have a couple of threads running that are doing this same sequence of steps.

In the thread environment, these threads could be (and probably are) running on separate cores. A dual core machine is very much like two computers that share a common memory. The cores operate independently and the programs (threads) run asynchronously (no timing relationship to each other).

Lets say we have thread A,B talking to this single display. moveA,DisplayA,moveB,displayB..no problem with that sequence. However, I think we can see that if the sequence that the display sees winds up being: moveA,moveB,displayA,displayB, there is a problem! Remember that A and B are completely independent of each other and this could happen. We get the wrong stuff displayed in the wrong place.

The actual situation is even worse than that. This move operation is not a single thing. Underneath it, work is going on to update various counters and pointers related to the single display. What happens if we are doing MoveB and before MoveA even finishes, the MoveB operation starts? Oh, geez are we in trouble! It is highly likely that whatever internal state that "move" is maintaining it is going to get screwed up and we don't wind up with either a "clean" A or B positioning.

As BrowserUk points out, the best solution here is for you to enforce serialization of the display commands by having only a single thread giving out these move,display commands. Some parts of the system, like the file/disk system are designed so that this is not necessary (it does the serialization to the actual hardware with some non-trivial code). The GUI is not designed that way, so you have to do that.

I don't find it surprising that your code runs for awhile before it fails. The threads will "drift" in time relationship to each other. If for no other reason that the OS needs to do other things and will "steal" some time from one of the cores to do it. If the operations are fast and relatively widely spaced out, it might take awhile for disaster to strike. But it inevitably will.

I hope that I didn't confuse the issue more and apologize for the wordy post.


In reply to Re: Weird Output with Threads and NCurses by Marshall
in thread Weird Output with Threads and NCurses by var121

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.