From: Kirk Haines Date: 2011-06-28T03:20:10+09:00 Subject: Re: Understanding Threading... On Mon, Jun 27, 2011 at 12:06 PM, Darryl L. Pierce wrote: > On Tue, Jun 28, 2011 at 02:28:07AM +0900, Luiz Angelo Daros de Luca wrote: >> Is your block using some C extension? >> In ruby<1.9, threads in ruby do >> not use system threads. So, if you call >> some external C command, no thread will run in ruby. In ruby>=1.9, it >> uses pthreads. > > So in 1.9 we should have _some_ semblance of multi-threading with > extensions? Yes, if you write your extension to take advantage of it. Do some searches for information on rb_thread_blocking_region(), and take a look at the source code for it in thread.c. Here are the relevant comments: /* * rb_thread_blocking_region - permit concurrent/parallel execution. * * This function does: * (1) release GVL. * Other Ruby threads may run in parallel. * (2) call func with data1. * (3) aquire GVL. * Other Ruby threads can not run in parallel any more. * * If another thread interrupts this thread (Thread#kill, signal deliverly, * VM-shutdown request, and so on), `ubf()' is called (`ubf()' means * "un-blocking function"). `ubf()' should interrupt `func()' execution. * * There are built-in ubfs and you can specify these ubfs. * However, we can not guarantee our built-in ubfs interrupt * your `func()' correctly. Be careful to use rb_thread_blocking_region(). * * * RUBY_UBF_IO: ubf for IO operation * * RUBY_UBF_PROCESS: ubf for process operation * * NOTE: You can not execute most of Ruby C API and touch Ruby objects * in `func()' and `ubf()' because current thread doesn't acquire * GVL (cause synchronization problem). If you need to do it, * read source code of C APIs and confirm by yourself. * * NOTE: In short, this API is difficult to use safely. I recommend you * use other ways if you have. We lack experiences to use this API. * Please report your problem related on it. * * Safe C API: * * rb_thread_interrupted() - check interrupt flag * * ruby_xalloc(), ruby_xrealloc(), ruby_xfree() - * if they called without GVL, acquire GVL automatically. */ Kirk Haines Software Engineer Engine Yard