From: Mathieu B. <mbl...@ru...> - 2007-10-27 22:14:34
|
Hi, I've tested the code that Guillaume Cottenceau is using in booh to do GUI stuff from within Ruby threads, in Fantasdic. I can report it to work well and it seems to have fixed "random" crashes that I was experiencing. Here's how I included it in my application: require "thread" module Gtk # Thread-safety stuff. # Loosely based on booh, by Guillaume Cottenceau. PENDING_CALLS_MUTEX = Mutex.new PENDING_CALLS = [] def self.thread_protect(&proc) if Thread.current == Thread.main proc.call else PENDING_CALLS_MUTEX.synchronize do PENDING_CALLS << proc end end end def self.thread_flush if PENDING_CALLS_MUTEX.try_lock for closure in PENDING_CALLS closure.call end PENDING_CALLS.clear PENDING_CALLS_MUTEX.unlock end end def self.init_thread_protect Gtk.timeout_add(100) do PENDING_CALLS_MUTEX.synchronize do for closure in PENDING_CALLS closure.call end PENDING_CALLS.clear end true end end end - I call Gtk.init_thread_protect once when I initialize my GUI stuff - Every time I need to update my GUI from withing a thread, I do: Gtk.thread_protect do { ... } - I call Gtk.thread_flush right after killing a thread when I ever happen to need kill a thread HTH, Mathieu Blondel |