Forum: Ruby-Gnome 2 Are the Ruby bindings threadsafe?

85991f138ede6236f35eb98da22b7b01?d=identicon&s=25 Marvin Gülker (quintus)
on 2013-09-15 17:56
(Received via mailing list)
Attachment: signature.asc (489 Bytes)
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

ASCII-Ribbon-Kampagne        ()   | ASCII Ribbon Campaign        ()
- Stoppt HTML-E-Mail         /\   | - Against HTML E-Mail        /\
- Stoppt proprietäre Anhänge      | - Against proprietary attachments
www.asciiribbon.org/index-de.html | www.asciiribbon.org
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2013-09-17 16:36
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 :
http://verboselogging.com/2009/06/01/gtk-and-ruby-...
85991f138ede6236f35eb98da22b7b01?d=identicon&s=25 Marvin Gülker (quintus)
on 2013-09-18 09:07
(Received via mailing list)
Attachment: signature.asc (489 Bytes)
Am Tue, 17 Sep 2013 16:36:38 +0200
schrieb Regis d'Aubarede <ruby-forum-incoming@andreas-s.net>:

> 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 :
> http://verboselogging.com/2009/06/01/gtk-and-ruby-...
>

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

Valete,
Marvin


--
Blog: 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
www.asciiribbon.org/index-de.html | www.asciiribbon.org
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.