Korundum and threads

Hi,

I’m trying to write a small daemon that would display a tray icon in the
KDE
task bar. The icon parts works perfectly well but starting the execution
of
the KDE application blocks all other threads. For example if I do
something
like:

Thread.new { print “.”; sleep(0.5) }
app = KDE::Application.new
tray = TrayHandler.new
tray.show
app.exec

Then the thread I started first gets stopped. I’m guessing there’s a
native
call here that doesn’t play well with Ruby green threads but is there a
way
to ask the Application to run in the background without disrupting the
execution of other threads?

Thanks!
Matthieu

On Dec 12, 2007, at 8:27 PM, Matthieu R. wrote:

Thread.new { print “.”; sleep(0.5) }
app = KDE::Application.new
tray = TrayHandler.new
tray.show
app.exec

Then the thread I started first gets stopped.

How certain are you that it’s your OS thats stopping the thread, and
not just the thread running out?

Try this for a thread

Thread.new { 1000000.times {|o| print o }

and post back the results.

-Ari

On Thu, 13 Dec 2007, Matthieu R. wrote:

Then the thread I started first gets stopped.

Personally I wish
Thread.abort_on_exception=true
was the default so things didn’t just silently vanish.

John C. Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : [email protected]
New Zealand

John C. wrote:

Personally I wish
Thread.abort_on_exception=true was the default so things didn’t just
silently vanish.

Then you lose Thread#value:

#Thread.abort_on_exception=true

th = Thread.new do
raise ArgumentError, “I’m sorry, but this is abuse”
end

begin
th.value
rescue => ex
puts ex.message
end

Matthieu R. wrote:

Then the thread I started first gets stopped. I’m guessing there’s a native
call here that doesn’t play well with Ruby green threads but is there a way
to ask the Application to run in the background without disrupting the
execution of other threads?

I don’t use Korundum, but generally GUI toolkits don’t work well out of
the box with Ruby’s green threads, because when they’re in the event
loop, non-GUI threads don’t get scheduled.

The usual solution is to use the GUI toolkit’s Timer class (eg
Qt::Timer, Wx::Timer) which fires at a short, regular interval. When it
fires, call something like Thread.pass or sleep, which will allow Ruby
to give execution time to other threads.

On Qt:
http://www.lesismore.co.za/Qt_and_Ruby

On wxRuby:
http://rubyforge.org/pipermail/wxruby-users/2007-April/003055.html

hth
alex