/* compile: gcc -Wall -g `pkg-config --cflags glib-2.0 gthread-2.0` main.c -o gthread-test `pkg-config --libs glib-2.0 gthread-2.0` */ #include <glib.h> #include <glib/gprintf.h> GAsyncQueue *qmsg, *qsig; const glong sender_dly =G_USEC_PER_SEC/10; gpointer sender( gpointer data); int main( int argc, char **argv) { GThread *s0, *s1; GTimeVal tv; GError *err =NULL; gint i; tv.tv_usec =G_USEC_PER_SEC/5; // that's 200 ms if( !g_thread_supported( )) g_thread_init( NULL); qmsg =g_async_queue_new( ); qsig =g_async_queue_new( ); if( NULL ==(s0 =g_thread_create( (GThreadFunc)sender, NULL, TRUE, &err))) { g_printf( "s0 create fail: %s\n", err->message); return 1; } if( NULL ==(s1 =g_thread_create( (GThreadFunc)sender, NULL, TRUE, &err))) { g_printf( "s1 create fail: %s\n", err->message); return 1; } g_async_queue_ref( qmsg); g_async_queue_ref( qsig); for( i =0; i <100; i++) { g_printf( "%d\n", GPOINTER_TO_INT( g_async_queue_timed_pop( qmsg, &tv))); } g_async_queue_unref( qmsg); g_async_queue_push( qsig, GINT_TO_POINTER( 1)); g_async_queue_push( qsig, GINT_TO_POINTER( 1)); g_thread_join( s0); g_thread_join( s1); g_async_queue_unref( qsig); return 0; } gpointer sender( gpointer data) { gpointer sig; gint niter =1; g_async_queue_ref( qmsg); g_async_queue_ref( qsig); while( NULL ==(sig =g_async_queue_try_pop( qsig))) { g_async_queue_push( qmsg, GINT_TO_POINTER( niter++)); g_usleep( sender_dly); } g_async_queue_unref( qmsg); g_printf( "sender( ): received on qsig: %d (\"I'm outta here\")\n", GPOINTER_TO_INT( sig)); g_async_queue_unref( qsig); return 0; }