Game Development Reference
In-Depth Information
Getting ready
Understanding of the asynchronous event concept is important before we proceed to
the implementation details. When we say asynchronous, we mean that something occurs
unpredictably and has no determined timing. For example, we cannot predict how long it will
take our task to download a URL—that is it; the task completes asynchronously and should
invoke a callback asynchronously.
How to do it…
1.
The message for us should be a method call. We will hide a method call behind
this interface:
class iAsyncCapsule: public iObject
{
public:
virtual void Invoke() = 0;
};
2. A pointer to an instance of such type represents a prepared method call. We deine
a queue of iAsyncCapsule with the following implementation:
class AsyncQueue
{
public:
AsyncQueue()
: FDemultiplexerMutex()
, FCurrentQueue( 0 )
, FAsyncQueues( 2 )
, FAsyncQueue( &FAsyncQueues[0] )
{ }
3.
Enqueue an event:
void EnqueueCapsule( const clPtr<iAsyncCapsule>& Capsule )
{
LMutex Mutex( &FDemultiplexerMutex );
FAsyncQueue->push_back( Capsule );
}
4.
The events demultiplexer, as described in the Reactor pattern ( http://
en.wikipedia.org/wiki/Reactor_pattern ):
void DemultiplexEvents()
{
CallQueue* LocalQueue = &FAsyncQueues[ FCurrentQueue ];
{
LMutex Lock( &FDemultiplexerMutex );
 
Search WWH ::




Custom Search