Skip to main content
gamedev.net
🔒 Locked

Event loop. How? [resolved]

Started by dsedov Sep 23, 2006 at 6:00 AM 3 replies 1k views
Original Post
dsedov
dsedov
Hi. Im writing a multithreaded application and each thread is going to have its event loop. I do understand and idea of while(GetEvents()) { ProcessEvents(events); } However, I dont understand the idea of GetEvents(). How can I make a function wait for events to appear in the event loop without having some while(1) { loop through events if event found -> break and return the event } This has a potential problem of loading CPU when there are no events. I can of course use something like "pause" but I dont want to do this. Is there any tutorials or rules on how to create your own event processing ? [Edited by - dsedov on September 23, 2006 5:42:46 PM]
dsedov
dsedov
Yes synchronization is a nice thing =) My question is more about custom even loop Im writing, and having problem with function which has to wait for events to come. The problem is how can I not load 100% CPU while the there are no events in stack?
dingojohn
dingojohn
If you look at the quake3 source code it is done this way:

In side the main game loop, the loop that does the timing, they have:

frameTime = EventLoop();


EventLoop() has:
while( 1 ) {   event = GetEvent();   if ( event.type == NONE )       return event.time;   else {      // event processing here   }}


GetEvent() is a function that works like this:
// do we already have an event to return?if ( eventQue.empty() != false )    event = eventQue.front();    eventQue.pop();    return event;}// Check for new eventswhile ( PollEvent )    QueEvent( PolledEvent )}// have we got new events?if ( eventQue.empty() != false )    event = eventQue.front();    eventQue.pop();    return event;}// no events, lets return an event event.type = NONE;event.time = GetCurrentSystemTime();return event;


I hope it could help in at least some way
incin
incin
You could use sockets or pipes, and then call select() to wait for events.
You could use semaphores:
ex:
event processor:
while()
{
mySemaphore.Acquire();
process all events
}

event creator:
createEvent()
{
//add event to some queue
if (queue was empty) mySemaphore.Post/Release()
}

Or you could use the windows message loop if you want to restrict yourself to windows.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.