GNOME Bugzilla – Bug 525192
100% CPU if run main loop with no IO sources
Last modified: 2008-03-31 18:34:43 UTC
Please describe the problem: Under Windows, if you run a main loop without watching a file handle or the fake Windows messages file handle (so you just want to wait on a timeout) then it will take up a 100% CPU instead of waiting for the timeout. Steps to reproduce: This is noticeable in the Clutter SDL port on Windows. Actual results: The program functions normally but eats up 100% CPU Expected results: Does this happen every time? Yes Other information:
Created attachment 108290 [details] [review] Patch to fix This patch fixes the problem by calling Sleep in g_poll if there are no I/O handles to watch but a timeout is given.
Thanks. Patch committed to glib-2-16 and trunk: 2008-03-31 Tor Lillqvist <tml@novell.com> Bug 525192 - 100% CPU if run main loop with no IO sources * glib/gmain.c (g_poll) [Win32]: Patch by Neil Roberts.
Shouldn't SleepEx be used to put the thread into an alertable wait state?
Good point, fixed. Thanks. Should the call to Sleep() in g_usleep() be changed to SleepEx(), too? Hard to say. I would say the logical answer would be yes, although code that cares for these things, or even uses User APCs, probably makes sure never to call g_usleep() anyway. I.e. put the SleepEx() in a loop, if it returns WAIT_IO_COMPLETION then re-sleep for the remaining time.
(In reply to comment #4) > Should the call to Sleep() in g_usleep() be changed to SleepEx(), too? I don't have a good sense of whether it should be yes or no. My sense is that adding it to g_usleep would make where APC's can be called less predictable. OTOH, I've think that the select call will allow APC's to run. Either way, it should be documented.