Korundum and threads

M

Matthieu Riou

[Note: parts of this message were removed to make it a legal post.]

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
 
T

thefed

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
 
J

John Carter

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 Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand
 
A

Alex Fenton

Matthieu said:
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
 
J

Joel VanderWerf

John said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top