PDA

View Full Version : Event loop. How?


DSedov
09-23-2006, 12:02 PM
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 ?

stew
09-26-2006, 05:53 PM
The best practice for running an event loop highly depends on what framework you are using (like Win32, wxWidgets, Qt, Carbon, etc). If you look for documentation or tutorials speicific to that framework you should find more info on the event loop.

Be aware that many frameworks don't have thread safe event loops! In many cases it might be better to have only one thread run the main event loop.

nurcc
09-30-2006, 07:07 AM
The main idea with most event loops is that instead of polling for events, they interact with the operating system in a more fundamental way. For example, they put themselves to sleep, and get woken up only when there are new events to process.

Depending on what your events are, and how rich your thread library is, you may be able to do something similar. The main idea would be to dispatch events via some sort of mutex or semaphore. That would allow you to modify a shared queue of events safely.

Again, it depends on what kind of events you're interested in for your application. I'd try searching for "producer consumer threads (http://www.google.com/search?hl=en&q=producer+consumer+threads&btnG=Google+Search)".

CGTalk Moderation
09-30-2006, 07:07 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.