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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |