# Are the Ruby bindings threadsafe?

**URL:** <https://www.ruby-forum.com/t/are-the-ruby-bindings-threadsafe/246223>\
**Category:** Ruby-Gnome 2\
**Created:** [September 15, 2013, 5:56pm UTC](https://www.ruby-forum.com/t/are-the-ruby-bindings-threadsafe/246223 "2013-09-15T17:56:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [September 15, 2013, 5:56pm UTC](https://www.ruby-forum.com/t/are-the-ruby-bindings-threadsafe/246223/1 "2013-09-15T17:56:48Z")

</div>

Hi there,

just a quick question… I’ve got a GUI program doing some networking  
stuff and want the GUI to react on what I get from my TCPSocket. To not  
block the GUI when the server/connection is slow, I intend to extract  
the networking stuff into a separate thread and then update the GUI  
from that thread. So, can I for example do something like this:

* * *

@textview = Gtk::TextView.new  
@textview.buffer.text = “Some initial text”

Thread.new do  
sleep 5  
@textview.buffer.text = “Some new text I got from somewhere else”  
end

# Continueing with other things in the main thread…

* * *

Is this possible or will I get segmentation faults or other unexpected  
results? If not, are there any other possibilities to update widgets  
from within another thread?

Valete,  
Marvin

–  
Blog: [http://www.quintilianus.eu](http://www.quintilianus.eu)

ASCII-Ribbon-Kampagne () | ASCII Ribbon Campaign ()

- Stoppt HTML-E-Mail /\ | - Against HTML E-Mail /\
- Stoppt proprietäre Anhänge | - Against proprietary attachments  
[The Ascii Ribbon Campaign official homepage - Deutsch](http://www.asciiribbon.org/index-de.html) | [www.asciiribbon.org](http://www.asciiribbon.org)

---

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [September 17, 2013, 4:36pm UTC](https://www.ruby-forum.com/t/are-the-ruby-bindings-threadsafe/246223/2 "2013-09-17T16:36:38Z")

</div>

For Ruiby, we use a Queue of bloc to be executed and a timer in  
main window for pooling the queue and intance\_eval the blocs received.

# Main thread side:

$ **mainwindow** =self  
ici=self  
GLib::Timeout.add(50) {  
while $ **queue**.size\>0  
mess= $ **queue**.pop  
ici.instance\_eval(&mess) rescue log(“#{$!} :\n #{  
$!.backtrace[0..3].  
join(”\n “)}”)  
end  
true  
}

# socket thread side:

def gui\_invoke(&blk)  
$ **queue**.push( blk )  
end

Also, see :

> **[Gtk And Ruby Threading Issues | Verbose Logging](https://verboselogging.com/2009/06/01/gtk-and-ruby-threading-issues)**
>
> Gtk has some threading issues in ruby. Here's how to get around them.

---

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [September 18, 2013, 9:07am UTC](https://www.ruby-forum.com/t/are-the-ruby-bindings-threadsafe/246223/3 "2013-09-18T09:07:05Z")

</div>

Am Tue, 17 Sep 2013 16:36:38 +0200  
schrieb Regis d’Aubarede [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> For Ruiby, we use a Queue of bloc to be executed and a timer in  
> main window for pooling the queue and intance\_eval the blocs received.
> 
> Also, see :  
> [Gtk And Ruby Threading Issues | Verbose Logging](http://verboselogging.com/2009/06/01/gtk-and-ruby-threading-issues)

Perfect! This was exactly what I was looking for. Thank you very much!

Valete,  
Marvin

–  
Blog: [http://www.quintilianus.eu](http://www.quintilianus.eu)

ASCII-Ribbon-Kampagne () | ASCII Ribbon Campaign ()

- Stoppt HTML-E-Mail /\ | - Against HTML E-Mail /\
- Stoppt proprietäre Anhänge | - Against proprietary attachments  
[The Ascii Ribbon Campaign official homepage - Deutsch](http://www.asciiribbon.org/index-de.html) | [www.asciiribbon.org](http://www.asciiribbon.org)
