GTimer为GLib的计时器,他可以计算自计时器开始到现在的时间差。下面的程序在程序运行开始创建计时器,通过时钟回调函数定期的输出程序运行的时间值。
#include
gboolean show_time(gpointer data);
int main(int argc,char** argv){
GMainLoop* main_loop = NULL;
guint i = 0;
GTimer* timer = g_timer_new();
main_loop = g_main_loop_new(NULL,FALSE);
g_timeout_add_seconds(1,(GSourceFunc)show_time,timer);
g_main_loop_run(main_loop);
g_main_loop_quit(main_loop);
return 0;
}
gboolean show_time(gpointer data){
GTimer* timer = (GTimer*)data;
gdouble seconds = g_timer_elapsed(timer,NULL);
guint hours = seconds / 3600;
guint minutes = (((guint)seconds) % 3600) / 60;
guint secs = (((guint)seconds) % 3600) % 60;
g_print("This program has been running for %02d:%02d:%02d\n",hours,minutes,secs);
return TRUE;
}
阅读(4482) | 评论(0) | 转发(0) |