From: Patrick Hurley Date: 2006-06-20T20:28:32+09:00 Subject: Re: C Threads and Ruby On 6/20/06, Kroeger, Simon (ext) wrote: > Hi, > > i know the ruby interpreter isn't thread safe and > it's not a good idea to call rb_* functions from more > than one thread. > > Nevertheless, is there a way to signal the interpreter > that i would like him to call one of my functions in a > c extension? (from another thread) > > The only way i figured out so far would be to create a > pipe, let a ruby thread sleep on the fd and put a byte > in the queue from c the wake the thread. (good, bad, ugly?) > > cheers > > Simon > > It depends what you are trying to do. I just worked up a patch to win32-service, that does this by creating a ruby thread in the C extension that polls against a simple integer guarded by a mutex -- when an event occurs it is signaled there and the correct call back is spun off in yet another ruby thread. I was worried this would be "expensive", but the rb_thread_polling method, worked like a charm. This idea could be easily extended to allow for an event queue that was filled by a native C thread and processed by a ruby thread. Note that calling any rb_XXXX from anything other than the "ruby thread" is a really bad idea. pth