GNOME Bugzilla – Bug 527214
g_timer_elapsed() returns random values
Last modified: 2011-10-04 21:36:45 UTC
GTimer uses internal uses GThread API (g_thread_gettime()) and therefore fails, when the thread system is not initialized yet. The API documention doesn't mention this requirement. The API docs should mention this requirement, and g_timer_new() should warn if the thread system is not initialized yet.
Created attachment 108953 [details] [review] [PATCH] Bug 527214 – g_timer_elapsed() returns random values. * glib/gtimer.c (g_timer_new()): Print warning if g_thread_init() has not been called yet. --- ChangeLog | 7 +++++++ docs/reference/ChangeLog | 7 +++++++ docs/reference/glib/tmpl/timers.sgml | 7 +++++++ glib/gtimer.c | 4 ++++ 4 files changed, 25 insertions(+), 0 deletions(-)
Good catch. The patch looks fine to me. Might be a case for g_warn_if_fail ?
Bug 527214 – g_timer_elapsed() returns random values. * glib/gtimer.c (g_timer_new()): Print warning if g_thread_init() has not been called yet. Patch by Mathias Hasselmann
Hm, I might be missing something, but it looks like g_thread_gettime does nothing related to threads at all... it just runs gettimeofday and returns the result. The only time you really need to init threads for gtimer is when you don't have nanosleep/nsleep, as far as I can see?
Yeah, I think I was too quick in accepting that patch. g_thread_gettime is supposed to work without threads. It should just work better when threads are enabled.
Mathias, what 'random values' were you seeing ? One problem could be if you start a timer before initializing the threads, and then stop it afterwards, because initializing threads changes the gettime implementation.
Created attachment 198258 [details] [review] gtimer: remove warnings about threads GTimer no longer uses the thread system for time information and g_thread_init() no longer does anything, so remove the doubly-untrue warning about these topics.