[Kde-bindings] Using threads with QtRuby

Melton, Ryan rmelton at ball.com
Fri Feb 5 14:48:30 UTC 2010

This is basically what I do to make threads work which seems to work great.  By calling Ruby's sleep method in a Timer, you allow the Ruby thread scheduler to run:

def MyMainWindow < Qt::MainWindow

  slots 'ruby_thread_timeout()'

  def ruby_thread_timeout

  def initialize

    # Do other things to setup your program

    # Enable ruby threading
    @ruby_thread_sleep_period = 0.01
    @ruby_thread_timer = Qt::Timer.new(self)
    connect(@ruby_thread_timer, SIGNAL('timeout()'), SLOT('ruby_thread_timeout()'))

-----Original Message-----
From: Richard Dale [mailto:rdale at foton.es] 
Sent: Friday, February 05, 2010 5:12 AM
To: KDE bindings for other programming languages
Subject: Re: [Kde-bindings] Using threads with QtRuby

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. 
Kde-bindings mailing list
Kde-bindings at kde.org

This message and any enclosures are intended only for the addressee.  Please  
notify the sender by email if you are not the intended recipient.  If you are  
not the intended recipient, you may not use, copy, disclose, or distribute this  
message or its contents or enclosures to any other person and any such actions  
may be unlawful.  Ball reserves the right to monitor and review all messages  
and enclosures sent to or from this email address.

More information about the Kde-bindings mailing list