[Kde-bindings] Using threads with QtRuby
Uriel
uriel at inbox.ru
Fri Feb 5 11:29:02 UTC 2010
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.
More information about the Kde-bindings
mailing list