static gboolean sort_queue (gpointer user_data) { static gint sorts = 0; static gpointer last_p = NULL; gpointer p; gboolean can_quit = FALSE; gint sort_multiplier; gint len; gint i; sort_multiplier = GPOINTER_TO_INT (user_data); if (SORT_QUEUE_AFTER) { PRINT_MSG (("sorting async queue...")); g_async_queue_sort (async_queue, sort_compare, NULL); sorts++; if (sorts >= sort_multiplier) { can_quit = TRUE; } g_async_queue_sort (async_queue, sort_compare, NULL); len = g_async_queue_length (async_queue); PRINT_MSG (("sorted queue (for %d/%d times, size:%d)...", sorts, MAX_SORTS, len)); } else { can_quit = TRUE; len = g_async_queue_length (async_queue); DEBUG_MSG (("printing queue (size:%d)...", len)); } for (i = 0, last_p = NULL; i < len; i++) { p = g_async_queue_pop (async_queue); DEBUG_MSG (("item %d ---> %d", i, GPOINTER_TO_INT (p))); if (last_p) { g_assert (GPOINTER_TO_INT (last_p) <= GPOINTER_TO_INT (p)); } last_p = p; } if (can_quit && QUIT_WHEN_DONE) { g_main_loop_quit (main_loop); } return !can_quit; }
int main(void) { g_thread_init(NULL); glyr_init(); atexit(glyr_cleanup); /* Create a new async queue */ FinishedNotify notify; notify.queue = g_async_queue_new(); notify.counter = 0; /* Initialize a new thread */ GError * err = NULL; GThread * apollo = g_thread_create(apollo_orbiter,¬ify,TRUE,&err); if(apollo != NULL) { /* Push a few jobs */ GlyrQuery one,two,three; build_queries(&one,&two,&three); g_async_queue_push(notify.queue,&one ); g_async_queue_push(notify.queue,&two ); g_async_queue_push(notify.queue,&three); #define ABORT_IMMEDIATELY FALSE #if ABORT_IMMEDIATELY /* Test if it really aborts immediately, if not it crashes :-) */ g_async_queue_push(notify.queue,(gpointer)0x2); #endif /* Terminate by pushing a special value */ g_async_queue_push(notify.queue,THREAD_TERMINATOR); #if ABORT_IMMEDIATELY /* Sort the THREAD_TERMINATOR before anything else. */ g_async_queue_sort(notify.queue,sort_async_queue_jobs,NULL); #endif /* Wait till he finished */ g_thread_join(apollo); } else { g_printerr("Could not create thread: %s\n",err->message); g_error_free(err); } g_async_queue_unref(notify.queue); return EXIT_SUCCESS; }