Game Development Reference
In-Depth Information
Figure 7.3
A cooperative multithreaded main loop.
A Hybrid Technique
What if we take the idea of putting multiple systems in their own discrete execution
modules but throw away all the problems with true concurrent execution? This gives
us the best of both worlds, keeping all of our different systems nice and decoupled
from each other and allowing them the illusion of being run simultaneously while
avoiding race conditions and other nasty threading issues. This technique is called
cooperative multitasking.
Cooperative multitasking is a mechanism where each process gets a little CPU time
in a round-robin fashion. It
s called cooperative because each process is responsible
for releasing control back to the calling entity. If a process goes into an infinite
loop, the entire system will hang. The trade-off for that weakness is that the system
is simple to design and extremely efficient.
Imagine a simple base class called Process with a single virtual method, VOnUpdate():
'
class Process
{
public:
virtual void VOnUpdate(unsigned long deltaMs) = 0;
};
You could create objects inheriting from this class and stick them in a master process list.
Every game loop, your code could traverse this list and call VOnUpdate() for each object:
typedef std::list<Process*> ProcessList;
ProcessList g_processList;
 
 
Search WWH ::




Custom Search