# Korundum and threads

**URL:** <https://www.ruby-forum.com/t/korundum-and-threads/122968>\
**Category:** Ruby\
**Created:** [December 13, 2007, 2:38am UTC](https://www.ruby-forum.com/t/korundum-and-threads/122968 "2007-12-13T02:38:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthieu\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@Matthieu\_R](https://www.ruby-forum.com/u/Matthieu_R)\
**Post date:** [December 13, 2007, 2:38am UTC](https://www.ruby-forum.com/t/korundum-and-threads/122968/1 "2007-12-13T02:38:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Matthieu\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@Matthieu\_R](https://www.ruby-forum.com/u/Matthieu_R)\
**Post date:** [December 13, 2007, 3:21am UTC](https://www.ruby-forum.com/t/korundum-and-threads/122968/2 "2007-12-13T03:21:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Matthieu\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@Matthieu\_R](https://www.ruby-forum.com/u/Matthieu_R)\
**Post date:** [December 13, 2007, 4:58am UTC](https://www.ruby-forum.com/t/korundum-and-threads/122968/3 "2007-12-13T04:58:58Z")

</div>

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 : removed\_email\_address@domain.invalid  
New Zealand

---

<div class="post-metadata">

**Author:** ![Matthieu\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@Matthieu\_R](https://www.ruby-forum.com/u/Matthieu_R)\
**Post date:** [December 13, 2007, 9:01am UTC](https://www.ruby-forum.com/t/korundum-and-threads/122968/4 "2007-12-13T09:01:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Matthieu\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@Matthieu\_R](https://www.ruby-forum.com/u/Matthieu_R)\
**Post date:** [December 13, 2007, 8:38am UTC](https://www.ruby-forum.com/t/korundum-and-threads/122968/5 "2007-12-13T08:38:25Z")

</div>

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](http://www.lesismore.co.za/Qt_and_Ruby)

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

hth  
alex
