Are procs thread-safe?

I’m assuming that it’s not good to make gtk2 calls in threads other than
the one that runs Gtk.main, and that not all gtk2 calls are reentrant.
But I have many socket-reading threads that need to update the GUI, and
I want to do this in a thread-safe way without resorting to timeouts.

My best idea so far is:

class GUI
def got_data(data) #called by other threads
queue << Proc.new do
#do some gtk2 stuff, use data
end
gtkThread.wakeup
end

def run_queued_procs
begin
loop do queue.pop(true).call end
rescue ThreadError
end
end

def gtk2_idle_function
run_queued_procs
end

def any_other_func
run_queued_procs
end
end

It seems like this will work (I haven’t tried it yet), but is it safe?
Can Proc.new be called at any time, including during modification of the
class? If not, would putting a mutex around every function be
sufficient?

If no, is there any other way to do this?

Thanks,

···


Tom Felker

I’m not confortable with an idea that’s not constantly being challenged.