[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Threads in RubyCocoa apps

Praful

2/28/2008 7:15:00 PM

Hi

I'm writing a GUI that starts a worker thread using Thread.start. The
Cocoa docs say that you should update the GUI using the main thread
only. However, the examples that ship with Leopard, eg SimpleApp, have
a thread that update the GUI from the thread without passing it to the
main thread.

I would like to update a textview (with log info) and a progress bar
from the worker thread. At the moment the worker thread is updating
the log quite happily -- it's passed a closure from the controller and
calls log.call(msg).

Given that the examples and Cocoa docs seem to contradict each other,
what is the correct of doing this? I'd very much appreciate sample
code since I've just started using RubyCocoa (which is great!).

Thanks

Praful
2 Answers

Alex Fenton

2/28/2008 9:33:00 PM

0

Praful wrote:

> I'm writing a GUI that starts a worker thread using Thread.start. The
> Cocoa docs say that you should update the GUI using the main thread
> only. However, the examples that ship with Leopard, eg SimpleApp, have
> a thread that update the GUI from the thread without passing it to the
> main thread.

When the Cocoa (Objective C) docs talk about threads, I would assume
they're referring to operating system threads. Ruby 1.8 threads are not
operating system threads - they're implemented within the interpreter.
Since all Ruby 1.8 threads are within the same operating system thread,
so it's usually safe to call GUI code in any thread.

A little bit of care can be needed, because ruby's thread scheduler can
potentially switch context in a place where it shouldn't. For example,
I've seen Carbon errors on OS X in wxRuby where it appears execution has
switched inside an operation like drawing to a device context.

alex

Praful

3/1/2008 5:01:00 PM

0

On Feb 28, 9:32 pm, Alex Fenton <a...@deleteme.pressure.to> wrote:
> Praful wrote:
> > I'm writing a GUI that starts a worker thread using Thread.start. The
> > Cocoa docs say that you should update the GUI using the main thread
> > only. However, the examples that ship with Leopard, eg SimpleApp, have
> > a thread that update the GUI from the thread without passing it to the
> > main thread.
>
> When the Cocoa (Objective C) docs talk about threads, I would assume
> they're referring to operating system threads. Ruby 1.8 threads are not
> operating system threads - they're implemented within the interpreter.
> Since all Ruby 1.8 threads are within the same operating system thread,
> so it's usually safe to call GUI code in any thread.
>
> A little bit of care can be needed, because ruby's thread scheduler can
> potentially switch context in a place where it shouldn't. For example,
> I've seen Carbon errors on OS X in wxRuby where it appears execution has
> switched inside an operation like drawing to a device context.
>
> alex

Many thanks Alex.