GTimers - do they need initialization ?



The following code is giving me strange results when I build it using MSVC++
and link to the binaries for glib-win32  (I've tried VC++8 and also falling
back to VC++6)

#include <glib/gtimer.h>
int main (int argc, char* argv[])
{
gulong microseconds;
int count;

    GTimer* pTimer = g_timer_new ();

    do {
         g_timer_elapsed (pTimer, &microseconds);
         count = (int) microseconds;

         printf ("Time elapsed = %d\n", count);

         g_usleep (1000000);
    } while (microseconds < 10000000);

     return 0;
}

Ignore the fact that I'm not cleaning up properly.  It's just a 'minimal
code' example.  I expected the outputted value of count'. to increment in
(approximately) 1 second intervals - e.g. the first value might be 0, the
next 1000000, then 2000000, then 3000000 etc (all approximate, of course).

If I run the above code in my debugger (and set a break point at the printf
line) I see seemingly random figures in the output, like this:-

Time elapsed = 781250
Time elapsed = 531250
Time elapsed = 968750
Time elapsed = 843750
Time elapsed = 968750
Time elapsed = 968750
Time elapsed = 93750
Time elapsed = 406250

that's an actual sequence.  At first, I assumed that running in the debugger
might be upsetting the timer so I decided to look at the output from just
running the program normally.  But if I run the program normally I just get
this repeated ad infinitum:-

Time elapsed = 0
Time elapsed = 0
Time elapsed = 0
Time elapsed = 0
Time elapsed = 0
Time elapsed = 0

I didn't call 'g_timer_start()' because the manual says that it gets called
automatically by g_timer_new().  However, even if I call it, I still see the
same output.

Should I have initialized something first?

John

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]