Game Development Reference
In-Depth Information
EventReaderProcess() : RealtimeProcess(ThreadProc)
{
IEventManager::Get()->VAddListener(
MakeDelegate(this, &EventReaderProcess::UpdateTickDelegate),
EvtData_Update_Tick::sk_EventType);
}
void UpdateTickDelegate(IEventDataPtr pEventData);
virtual void VThreadProc(void);
protected:
static ThreadSafeEventQueue m_RealtimeEventQueue;
};
void EventReaderProcess::UpdateTickDelegate(IEventDataPtr pEventData)
{
IEventDataPtr pEventClone = pEventData->VCopy();
m_RealtimeEventQueue.push(pEventClone);
}
DWORD g_EventsRead = 0;
void EventReaderProcess::VThreadProc(void)
{
// wait for all threads to end
while (m_EventsRead < 100000)
{
IEventDataPtr e;
if (m_RealtimeEventQueue.try_pop(e))
++m_EventsRead;
}
Succeed();
}
Note that this process has its own thread-safe event queue, to make this example
much simpler. The Event Manager has a real-time event queue of its own to receive
events from real-time processes, but it doesn ' t have one to manage sending events to
real-time processes. It would be a great assignment to refactor this system so that the
Event Manager could manage the reception and sending of all events, real-time or
otherwise. This trivial example simply waits until 100,000 EvtData_Update_Tick
events are seen in the thread-safe event queue. One thing you should notice is that
when the event is received by the delegate, it is copied before being sent in to the
real-time event queue. That is because shared_ptr<> objects are not thread safe,
so the event is cloned to avoid any problems.
Search WWH ::




Custom Search