- #include <gio/gio.h>
- #include <stdlib.h>
- #include <string.h>
-
- gpointer current_data = NULL;
- GMutex data_mutex;
- GCond data_cond;
-
- void push_data(gpointer data) {
- g_mutex_lock(&data_mutex);
- current_data = data;
- g_cond_signal(&data_cond);
- g_mutex_unlock(&data_mutex);
- }
-
- gpointer pop_data_timed(void) {
- gint64 end_time;
- gpointer data;
-
- g_print("start timer:\t%ld\n", g_get_monotonic_time());
-
- g_mutex_lock(&data_mutex);
-
- end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
- g_print("wait until:\t%ld\n", end_time);
- while (!current_data)
- if (!g_cond_wait_until(&data_cond, &data_mutex, end_time)) {
- g_print("timeout:\t%ld\n", g_get_monotonic_time());
- // timeout has passed.
- g_mutex_unlock(&data_mutex);
- return NULL;
- }
-
- // there is data for us
- data = current_data;
- current_data = NULL;
-
- g_mutex_unlock(&data_mutex);
-
- return data;
- }
-
- int main(int argc, char **argv) {
- GApplication *app;
- int status;
-
- app = g_application_new("org.gtk.TestApplication", G_APPLICATION_HANDLES_COMMAND_LINE);
- g_signal_connect(app, "command-line", G_CALLBACK(pop_data_timed), NULL);
- g_application_set_inactivity_timeout(app, 10000);
-
- status = g_application_run(app, argc, argv);
-
- g_object_unref(app);
-
- return status;
- }