Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gnome/glib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ add_test(
NAME hello-glib
COMMAND hello-glib
)

add_executable(g-mainloop-app g-mainloop-app.c)
target_link_libraries(g-mainloop-app PRIVATE PkgConfig::GLIB)
41 changes: 41 additions & 0 deletions gnome/glib/g-mainloop-app.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright © 2026 Chee Bin HOH. All rights reserved.
*
* @file g-mainloop-app.c
* @brief glib mainloop app with timer.
*/

#include <glib.h>

static gboolean on_timeout(gpointer user_data) {
GMainLoop *mainloop = (GMainLoop *)user_data;

static int count = 5;

if (count < 1) {
g_print("quitting...\n");
g_main_loop_quit(mainloop);

return G_SOURCE_REMOVE;
} else {
g_print("timer is fired, count down %d to zero...\n", count);
}

count--;

return G_SOURCE_CONTINUE;
}

int main(int argc, char *argv[]) {
GMainLoop *mainloop = NULL;

mainloop = g_main_loop_new(NULL, FALSE);

g_timeout_add(1000, on_timeout, mainloop);

g_main_loop_run(mainloop);

g_main_loop_unref(mainloop);

return 0;
}