Game Development Reference
In-Depth Information
5.
This is an even-odd trick to prevent copying the entire queue. We keep two queues
and switch between them:
FCurrentQueue = ( FCurrentQueue + 1 ) % 2;
FAsyncQueue = &FAsyncQueues[ FCurrentQueue ];
}
6.
Note the mutex's scope above. We should not invoke callbacks while the mutex is
locked:
for ( CallQueue::iterator i = LocalQueue->begin();
i != LocalQueue->end(); ++i )
(*i)->Invoke();
LocalQueue->clear();
}
private:
size_t FCurrentQueue;
typedef std::vector< clPtr<iAsyncCapsule> > CallQueue;
std::vector<CallQueue> FAsyncQueues;
CallQueue* FAsyncQueue;
Mutex FDemultiplexerMutex;
};
How it works…
We start two threads. One handles incoming events by making a call to the
DemultiplexEvents() function in an endless loop:
class ResponseThread: public iThread, public AsyncQueue
{
public:
virtual void Run() { while (true) { DemultiplexEvents(); } }
};
ResponseThread* Responder;
And the other thread produces asynchronous events:
class RequestThread: public iThread
{
public:
virtual void Run()
{
while ( true )
{
Responder->EnqueueCapsule( new TestCall() );
 
Search WWH ::




Custom Search