# Threads in RubyCocoa apps

**URL:** <https://www.ruby-forum.com/t/threads-in-rubycocoa-apps/130501>\
**Category:** Ruby\
**Created:** [February 28, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/threads-in-rubycocoa-apps/130501 "2008-02-28T20:20:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![praful](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@praful](https://www.ruby-forum.com/u/praful)\
**Post date:** [February 28, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/threads-in-rubycocoa-apps/130501/1 "2008-02-28T20:20:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![praful](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@praful](https://www.ruby-forum.com/u/praful)\
**Post date:** [March 1, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/threads-in-rubycocoa-apps/130501/2 "2008-03-01T18:05:38Z")

</div>

On Feb 28, 9:32 pm, Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![praful](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@praful](https://www.ruby-forum.com/u/praful)\
**Post date:** [February 28, 2008, 10:41pm UTC](https://www.ruby-forum.com/t/threads-in-rubycocoa-apps/130501/3 "2008-02-28T22:41:26Z")

</div>

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
