GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat May 30, 2015 2:32 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Wake up main loop from worker thread
PostPosted: Wed Apr 01, 2015 11:15 am 
Offline
GTK+ Geek

Joined: Thu Nov 12, 2009 1:49 pm
Posts: 54
Hello,

I'm having difficulties waking up my main loop from a worker thread. I'm doing the following in the worker thread in order to have myfunc() called from the main thread:

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


However, myfunc() isn't called immediately. I have to move the mouse over some of my windows in order to wake up the main loop and then it will call myfunc(). Without moving the mouse or doing anything else that will cause the main loop to wake up, myfunc() won't get called. I also tried the following but it doesn't work either:

Code:
g_timeout_add(1, myfunc, myargs);
g_main_context_wakeup(NULL);


This shows the same behaviour as above. myfunc() will only get called if I do something that causes the main loop to wake up.

The main loop looks like this:

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


Any ideas what is wrong here?

Thanks!


Top
 Profile  
 
 Post subject: Re: Wake up main loop from worker thread
PostPosted: Wed Apr 01, 2015 12:43 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 866
Location: UK
Hallo,

Can you give more information on which versions of the libraries you are using. On older versions of GLib you must initialise threading before you can use any threading within it. How are you initialising GLib or GTK if you are using it?

GTK/GDK is not thread safe, but thread aware. Although calling GTK/GDK from more than one thread is deprecated.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Wake up main loop from worker thread
PostPosted: Wed Apr 01, 2015 1:28 pm 
Offline
GTK+ Geek

Joined: Thu Nov 12, 2009 1:49 pm
Posts: 54
It's an older version of GTK2 but I don't know the exact version. It came with openSUSE 11 which I installed in 2008 or so and never updated it since :)

I know that GTK isn't thread-safe, that's why I want to delegate this function call from the worker thread to the main thread. The only initialization I'm doing is calling gtk_init() in the main thread...

So should I just try calling g_thread_init() in the main thread as well?


Top
 Profile  
 
 Post subject: Re: Wake up main loop from worker thread
PostPosted: Wed Apr 01, 2015 1:40 pm 
Offline
GTK+ Geek

Joined: Thu Nov 12, 2009 1:49 pm
Posts: 54
Ok, doing a

Code:
g_thread_init(NULL);


immediately after gtk_init() solves the problem and it's working fine now. Thanks for the hint!


Top
 Profile  
 
 Post subject: Re: Wake up main loop from worker thread
PostPosted: Wed Apr 01, 2015 2:20 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 866
Location: UK
Good that it works now. I take it that you are using an older version of GLib where you need to initialise it using g_thread_init() to get threads working.

If you are also using GTK/GDK you will also need to initialise the internal locking if you are calling it from more than one thread. Emmanuele Bassi has a tutorial at https://www.bassi.io/articles/2014/08/27/gdk-and-threads/ if you need that.

_________________
E.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group