c - Running multiple concurrent GMainLoops -
are users of glib allowed run multiple gmainloop
instances concurrently in multiple threads, each thread running own instance? i've found "yes" , "no" answers on place. realize question has been asked before in forum (december 2011).
however, able run 2 gmainloop
instances @ same time without apparent issue. test code simple:
- create
gmainloop
inmain()
- create timeout source default context , main loop using
g_timeout_add
- create gthread in main()
- run main loop using
g_main_loop_run
- [thread context]: create context using
g_main_context_new
- [thread context]: set context thread default using
g_main_context_push_thread_default
- [thread context]: create loop using
g_main_loop_new
, give new context - [thread context]: create timeout source, , attach new context via
g_source_attach
. - [thread_context]: have thread invoke
g_main_loop_run
doing this, see both instances of gmainloop
working fine. timeout callbacks invoked correctly, , later calls g_main_loop_quit work expected.
so, looks not problem have multiple gmainloop
instances working concurrently. perhaps haven't exercised api enough grasp situation. there definitive answer question?
also, here's actual test code if cares look:
#define thread_timeouts (20) #define main_timeous (1) typedef struct timeout_struct { int i; int max; gmainloop *loop; char *name; } timeout_struct; gboolean timeout_callback(gpointer data) { timeout_struct *pstimeout = (timeout_struct *)data; pstimeout->i++; if (pstimeout->i == pstimeout->max) { if (pstimeout->max == thread_timeouts) { g_main_loop_quit( (gmainloop*)pstimeout->loop ); } return false; } return true; } void* thread_function(void *data) { gmaincontext *ps_context; gmainloop *ps_loop; gsource *ps_timer; timeout_struct stimeout; ps_context = g_main_context_new(); g_main_context_push_thread_default(ps_context); ps_loop = g_main_loop_new(ps_context, false); stimeout.i = 0; stimeout.max = thread_timeouts; stimeout.loop = ps_loop; stimeout.name = "thread"; ps_timer = g_timeout_source_new_seconds(1); g_source_set_callback(ps_timer, timeout_callback, &stimeout, null); g_source_attach(ps_timer, ps_context); g_main_loop_run(ps_loop); g_main_loop_quit( (gmainloop*)data ); return null; } /* * main boots thread, starts gmainloop. thread runs * gmainloop. thread sets timer fires ten times , main sets * timer fires 2 times. thread quits , * , other main l * * * */ int main() { gthread *ps_thread; gmainloop *loop; timeout_struct stimeout; loop = g_main_loop_new ( null , false ); stimeout.i = 0; stimeout.max = main_timeous; stimeout.loop = loop; stimeout.name = "main"; // add source default context g_timeout_add (1 , timeout_callback, &stimeout); ps_thread = g_thread_new("thread", thread_function, loop); g_main_loop_run (loop); g_main_loop_unref(loop); }
the book "foundations of gtk+ development" states this:
the glib main loop implemented number of structures, allow multiple instances run concurrently.
so, given this, test code, , link posted in comment above have definitive answer question.
namely: multiple threads may have own gmaincontext & gmainloop, , able independently run these loops in concurrent fashion.
Comments
Post a Comment