Search code examples
cmultithreadinggtkglib

GTK: Delegate function call to main thread


I'm having some problems delegating a function call to the main thread from a worker thread. Since I can't call into GTK from the worker thread, I want to delegate this call to the main thread to do it for me. The function should be called as soon as possible on the main thread.

I tried the following:

g_idle_add(myfunc, myargs);
g_main_context_wakeup(NULL);

But it doesn't work. myfunc() is not called immediately but only when some other input arrives that seems to trigger the main loop. I don't really see why it doesn't work. Shouldn't g_main_context_wakeup() wakeup the main loop?

My main loop looks like this:

loop = g_main_loop_new(NULL, FALSE);
while(!quit) g_main_context_iteration(NULL, TRUE); 

Any ideas?


Solution

  • To answer my own question, this didn't work because I'm on an old GTK version which requires multithreading to be explicitly enabled by calling g_thread_init() first. Once I call g_thread_init(), g_main_context_wakeup() works fine now.