# Threading questions

**URL:** <https://www.ruby-forum.com/t/threading-questions/160107>\
**Category:** Ruby-Gnome 2\
**Created:** [February 11, 2009, 9:25pm UTC](https://www.ruby-forum.com/t/threading-questions/160107 "2009-02-11T21:25:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [February 11, 2009, 9:25pm UTC](https://www.ruby-forum.com/t/threading-questions/160107/1 "2009-02-11T21:25:51Z")

</div>

Hi

I have read the Tip about using Ruby threads with GTK  
([http://ruby-gnome2.sourceforge.jp/hiki.cgi?tips\_threads](http://ruby-gnome2.sourceforge.jp/hiki.cgi?tips_threads)), and I have  
a few questions that I hope you can help me with.

First, since all Ruby code (in 1.8.6 at least) runs from within the  
same system thread, why does GTK care about which Ruby thread it is  
called from? Don’t all Ruby threads just appear to be one thread to  
GTK?

Second, the tip says that we must call GTK methods from within the  
main thread. Is it possible instead to use the Gdk::Threads.enter and  
Gdk::Threads.leave to surround GTK code? Other GTK bindings (PyGTK and  
Java-GNOME) have used these methods:

```
http://www.pardon-sleeuwaegen.be/antoon/python/page0.html
http://research.operationaldynamics.com/blogs/andrew/software/gnome-desktop/gtk-thread-awareness.html

```

And if it is indeed possible to use these methods, can we use a  
similar technique to Java-GNOME to make Ruby-GNOME threadsafe?

```
http://research.operationaldynamics.com/blogs/andrew/software/java-gnome/java-gnome-4.0.3-release.html

```

Finally, does anyone know a quick way to make Ruby-GNOME segfault with  
threads? Because I’d like to test out how these things work for  
myself, so I can understand the techniques better.

many thanks,  
Dan

* * *

Daniel L.

[www.daniellucraft.com/blog](http://www.daniellucraft.com/blog)  
[twitter.com/danlucraft](http://twitter.com/danlucraft)

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [February 11, 2009, 10:25pm UTC](https://www.ruby-forum.com/t/threading-questions/160107/2 "2009-02-11T22:25:28Z")

</div>

Hi,

On Wed, Feb 11, 2009 at 9:25 PM, Daniel L.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Hi
> 
> I have read the Tip about using Ruby threads with GTK  
> ([http://ruby-gnome2.sourceforge.jp/hiki.cgi?tips\_threads](http://ruby-gnome2.sourceforge.jp/hiki.cgi?tips_threads))

Unfortunately the solution described in this page is not very  
“environment”  
friendly. As far as I understand, It forces the main loop to wake up 10  
times per second and prevents the CPU to remain in idle, low-consumption  
mode. This is still acceptable for a desktop-PC but totally unacceptable  
for  
a mobile device because the battery would be sucked up very fast.

(Would you have thought that you can have an impact on global warming  
with  
your little Ruby app? 😉

For lack of anything better, I’m still using the solution described in  
this  
page in my application but I hope someone come up with an ideal solution  
because I would like my application to run on Maemo.

Mathieu

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [February 12, 2009, 8:51am UTC](https://www.ruby-forum.com/t/threading-questions/160107/3 "2009-02-12T08:51:39Z")

</div>

Right, right. It’s a pretty minimal usage but it could be significant  
in a mobile app.

I’m going to port this example to Ruby:

[http://www.johnstowers.co.nz/blog/index.php/2007/03/12/threading-and-pygtk/](http://www.johnstowers.co.nz/blog/index.php/2007/03/12/threading-and-pygtk/)

Maybe I can get some segfaults that way.

best,  
Dan

* * *

Daniel L.

[www.daniellucraft.com/blog](http://www.daniellucraft.com/blog)  
[twitter.com/danlucraft](http://twitter.com/danlucraft)

2009/2/11 Mathieu B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [February 22, 2009, 12:23pm UTC](https://www.ruby-forum.com/t/threading-questions/160107/4 "2009-02-22T12:23:14Z")

</div>

Hi,

From: Daniel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Wed, 11 Feb 2009 20:25:11 +0000

> First, since all Ruby code (in 1.8.6 at least) runs from within the  
> same system thread, why does GTK care about which Ruby thread it is  
> called from? Don’t all Ruby threads just appear to be one thread to  
> GTK?

Ruby 1.8 threads are implemented by replacing the stack’s contents.  
So during different ruby thread is running, the stack is inconsistent  
from GTK viewpoint. If GTK code meets this inconsistent stack,  
segmentation fault could happen.

Regards,

– Masahiro S.

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [April 8, 2009, 3:10pm UTC](https://www.ruby-forum.com/t/threading-questions/160107/5 "2009-04-08T15:10:33Z")

</div>

2009/2/22 Masahiro S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Ruby 1.8 threads are implemented by replacing the stack’s contents.  
> So during different ruby thread is running, the stack is inconsistent  
> from GTK viewpoint. If GTK code meets this inconsistent stack,  
> segmentation fault could happen.

I see. That makes a lot more sense, thankyou.

Dobai-Pataky Bálint wrote:

> hi, i attached an old test of mine,  
> it does not segfault with 0.18.1,  
> but exits with an error.  
> hope it helps.

Thanks. I get this:

GLib:ERROR:/build/buildd/glib2.0-2.18.2/glib/gmain.c:2147:g\_main\_dispatch:  
assertion failed: (current-\>dispatching\_sources ==  
&current\_source\_link)  
Aborted

Does this mean that GLib now has an improved error handling for these  
thread failure situations? Would it be possible for us to catch this  
error somehow in Ruby code? That would certainly make debugging  
easier, especially when you don’t control all the code you’re running.

Thanks for the responses guys, sorry it took me so long to get around  
to thanking you.

best,  
Dan

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [February 12, 2009, 9:38pm UTC](https://www.ruby-forum.com/t/threading-questions/160107/6 "2009-02-12T21:38:21Z")

</div>

hi, i attached an old test of mine,  
it does not segfault with 0.18.1,  
but exits with an error.  
hope it helps.

balint
