Application Planning & Design - The quick 101
by
Published on 04-11-2011 09:03 PM
Number of Views: 24481
Now thats all fine, but that was a monolithic program AKA it only does one thing at any one time. Lets take a strategy game now, each players AI is in a separate thread, how does it fit together, its now not just a single branch but how the seperate threads interact with each other - this is harder because its not just getting a thread to talk and one to listen it has to happen at the same time!
For this I would draw rectangles for each thread and take it from there, a bit like this (main loop only):
(This should be in blocks but the editor does some weird stuff with spacing, sorry).
Code:
|---------------------------| |---------------------------| |---------------------------|
| Get human input and | | Work out what the | | Work out what the |
| wait() | | next action is and wait | | next action is and wait |
*------------------------------------------------------------------------------------------*
| Send to draw queue | | Send to draw queue | | Send to draw queue |
And so on...
The point is, each thread has an all important thread that breaks it after each crucial instruction - if that third box (thread) is on another computer and it sends player input to the main thread and it is busy drawing and not listening on the port for input - it won't arrive. Anomalies can then occur in the graphics for example if threads 1 and 3 have finished their draw cycles and are waiting for the next cycle, with all those graphics nicely on screen before thread 2 comes and draws itself onto the screen... Flickering can occur here, and in some instances artifacts depending on how the blending is set but that is a different matter.
The solution - simpler than it seems. Either, have a
Code:
ReadyToAdvance : Boolean;
and a
Code:
while (ThreadType.ReadyToAdvance = False) do delay(10);
Or something similar to prevent things coming out of sync. This is crucial when dealing with threads - and yes my examples may not be the best out there - a computational example for encryption would have been better: if each thread works on encrypting a block of data and the main thread pieces it back together as it is recieved and one thread lags for a moment due to high CPU usage and doesn't get that block sent to the main thread in time - big disaster: encrypted text out of order, in say AES-256, is pretty much lost forever. Bad.
vBulletin Message