/* 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; }
Showing posts with label glib. Show all posts
Showing posts with label glib. Show all posts
2006-08-24
GThread & GAsyncQueue stuff
GList stuff
/* compile: gcc -Wall -g `pkg-config --cflags glib-2.0` main.c -o glist-test `pkg-config --libs glib-2.0` */ #include <glib.h> #include <glib/gprintf.h> void print_list( GList *list); GList *free_elem( GList *list, gint data); int main( int argc, char **argv) { GList *list =NULL; gint i; for( i =1; i <8; i++) list =g_list_append( list, GINT_TO_POINTER( i)); print_list( list); list =free_elem( list, 2); print_list( list); list =free_elem( list, 4); print_list( list); list =free_elem( list, 6); print_list( list); g_list_free( list); print_list( list); return 0; } void print_list( GList *list) { GList *l; g_printf( "%p: ", list); for( l =list; l !=NULL; l =g_list_next( l)) g_printf( "0x%x -> ", GPOINTER_TO_INT( l->data)); g_printf( "NULL\n\n"); } GList *free_elem( GList *list, gint data) { GList *l =g_list_find( list, GINT_TO_POINTER( data)); list =g_list_remove_link( list, g_list_find( list, GINT_TO_POINTER( data))); g_list_free_1( l); print_list( l); return list; }Running with:
valgrind --leak-check=yes --show-below-main=yes --show-reachable=yes ./glist-testgives:
... 0x41df000: 0x1 -> 0x2 -> 0x3 -> 0x4 -> 0x5 -> 0x6 -> 0x7 -> NULL 0x41df010: 0x0 -> NULL 0x41df000: 0x1 -> 0x3 -> 0x4 -> 0x5 -> 0x6 -> 0x7 -> NULL 0x41df030: 0x41df010 -> NULL 0x41df000: 0x1 -> 0x3 -> 0x5 -> 0x6 -> 0x7 -> NULL 0x41df050: 0x41df030 -> NULL 0x41df000: 0x1 -> 0x3 -> 0x5 -> 0x7 -> NULL 0x41df000: 0x41df050 -> NULL ==5433== ==5433== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1) ==5433== malloc/free: in use at exit: 4,556 bytes in 8 blocks. ==5433== malloc/free: 8 allocs, 0 frees, 4,556 bytes allocated. ==5433== For counts of detected errors, rerun with: -v ==5433== searching for pointers to 8 not-freed blocks. ==5433== checked 83,028 bytes. ==5433== ==5433== 992 bytes in 4 blocks are still reachable in loss record 1 of 2 ==5433== at 0x401C970: memalign (vg_replace_malloc.c:332) ==5433== by 0x401C9FB: posix_memalign (vg_replace_malloc.c:384) ==5433== by 0x4063068: (within /usr/lib/libglib-2.0.so.0.1000.3) ==5433== by 0x4063E20: g_slice_alloc (in /usr/lib/libglib-2.0.so.0.1000.3) ==5433== by 0x404B8AB: g_list_append (in /usr/lib/libglib-2.0.so.0.1000.3) ==5433== by 0x80484DD: main (main.c:21) ==5433== by 0x40C2EA3: __libc_start_main (in /lib/tls/libc-2.3.6.so) ==5433== by 0x8048420: ??? (start.S:119) ==5433== ==5433== ==5433== 3,564 bytes in 4 blocks are still reachable in loss record 2 of 2 ==5433== at 0x401C7AA: calloc (vg_replace_malloc.c:279) ==5433== by 0x4054FC9: g_malloc0 (in /usr/lib/libglib-2.0.so.0.1000.3) ==5433== by 0x4062CDF: (within /usr/lib/libglib-2.0.so.0.1000.3) ==5433== by 0x4063C1E: g_slice_alloc (in /usr/lib/libglib-2.0.so.0.1000.3) ==5433== by 0x404B8AB: g_list_append (in /usr/lib/libglib-2.0.so.0.1000.3) ==5433== by 0x80484DD: main (main.c:21) ==5433== by 0x40C2EA3: __libc_start_main (in /lib/tls/libc-2.3.6.so) ==5433== by 0x8048420: ??? (start.S:119) ==5433== ==5433== LEAK SUMMARY: ==5433== definitely lost: 0 bytes in 0 blocks. ==5433== possibly lost: 0 bytes in 0 blocks. ==5433== still reachable: 4,556 bytes in 8 blocks. ==5433== suppressed: 0 bytes in 0 blocks.So
free_elem( )
does it's job..? Gott weiß.
Subscribe to:
Posts (Atom)