Game Development Reference
In-Depth Information
}
popped_value=the_queue.front();
the_queue.pop();
}
};
The m_dataPushed handle is a mechanism that allows one thread to notify another
thread that a particular condition has become true. Without it, a reader thread
manipulating the queue would have to lock the mutex, check the queue, find that it
was empty, release the lock, and then find a way to wait for a while before checking it
all over again. When WaitForSingleObject() is called, the thread blocks until
there is something to read. The call to PulseEvent signals there is something
there. This increases concurrency immensely.
Here
'
s how the EventManager class you saw in Chapter 11,
Game Event Manage-
ment,
needs to change to be able to receive events from real-time processes:
typedef concurrent_queue<IEventDataPtr> ThreadSafeEventQueue;
class EventManager : public IEventManager
{
// Add a new method and a new member:
public:
virtual bool VThreadSafeQueueEvent ( const IEventDataPtr &pEvent );
private:
ThreadSafeEventQueue m_RealtimeEventQueue;
}
bool EventManager::VThreadSafeQueueEvent ( const IEventDataPtr &pEvent )
{
m_RealtimeEventQueue.push(inEvent);
return true;
}
The concurrent queue template is used to create a thread-safe queue for IEvent-
DataPtr objects, which are the mainstay of the event system. The method VThread-
SafeQueueEvent() can be called by any process in any thread at any time. All that
remainsistoaddthecodeto EventManager::VTick() to read the events out of the
queue:
bool EventManager::VUpdate ( unsigned long maxMillis )
{
unsigned long curMs = GetTickCount();
unsigned long maxMs =
Search WWH ::




Custom Search