glib庫簡單計時器的實現

#include
#include <glib.h>

#define INTERVALS    10    //間隔10ms
#define COUNTER        1000    //計時間隔數

GMainLoop *loop;
gint count = COUNTER;

gboolean callback(gpointer arg)
{
    count--;

    if( 0 == count%100 )
    {
        printf("%d \n", count/100);
    }
    if ( 0 == count )
    {
        g_main_loop_quit(loop);
        return FALSE;
    }    
    return TRUE;
}

int main()
{
    if( 0 == g_thread_supported() )
    {
        g_thread_init(NULL);
    }
    
    loop = g_main_loop_new(NULL, FALSE);
    
    g_timeout_add(INTERVALS, callback, NULL);  /*每隔INTERVALS秒鐘調用一次回調函數,當調用COUNTER次後,執行g_main_loop_quit(loop),退出事件循環*/
    g_main_loop_run(loop);
    
    g_main_loop_unref(loop);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章