Threads in RubyCocoa apps

P

Praful

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
 
A

Alex Fenton

Praful said:
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
 
P

Praful

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.
 

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

Similar Threads

RubyCocoa question 2
[ANN] RubyCocoa 0.13.2 0
[ANN] RubyCocoa 0.13.0 0
[ANN]RubyCocoa 0.4.2 0
[ANN] RubyCocoa 0.11.0 3
Rubycocoa script application without nib 0
[ANN] RubyCocoa 0.11.1 0
[ANN] RubyCocoa 0.12.0 2

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top