[Kde-bindings] Using threads with QtRuby

Richard Dale rdale at foton.es
Fri Feb 5 12:11:35 UTC 2010


On Friday 05 February 2010 11:29:02 am Uriel wrote:
> Hello.
> 
> I'm playing with QtRuby and now a bit stuck with threads. I know, that
> Ruby doesn't use native threads and do all the scheduling inside the VM.
> It allows to use gui elements from non-gui threads which is not possible
> with regular threads and it's quite an interesting side-effect.
> But, in fact, the threads themselves seems to be broken because of the
> main Qt's event loop (that's just an assumption anyway).
> I haven't managed to find out the reason of such behaviour but threads
> can work at very low priority under some unknown conditions or not work
> at all what is much more common case.
> 
> At first I thought that it's beacuse binding makes call to the native
> method QCoreApplication::exec and waits for it to return before
> performing further scheduling.
> But then I tried the GTK2 bindings and to my surprise it works just
> fine. Below is the sample code.
> 
> require 'Qt'
> 
> app = Qt::Application.new []
> 
> button = Qt::PushButton.new 'Qt button'
> button.connect(SIGNAL 'clicked()'){ Thread.new{ loop do p "From Qt";
> sleep 1; end } }
> 
> button.show
> 
> app.exec
> 
> This code produces no output unless call to thread's join method
> explicitly but that of course blocks the ui.
> 
> require 'gtk2'
> 
> button = Gtk::Button.new 'Gtk button'
> button.signal_connect('clicked'){ Thread.new{ loop do p "From Gtk";
> sleep 1; end } }
> 
> window = Gtk::Window.new
> window.add button
> 
> button.show
> window.show
> 
> Gtk.main
> 
> But this code works as expected in spite of the GTK's event loop.
> 
> So I'm wondering if anybody tried to work with threads in QtRuby and how
> successfully they were with this.
> 
> Thanks for your attention.
Thats strange because Qt on my system uses to same glib event loop as GTK uses 
and QtRuby still doesn't work well with Ruby threads as you say. Maybe GTK 
ruby is using some hook to periodically run code in threads I don't know.

Threads in Ruby 1.8.x are really just broken, but it might be possible to get 
QtRuby and Ruby threads working with Ruby 1.9.x. 



More information about the Kde-bindings mailing list