From: Brent Roman Date: 2007-12-23T15:13:09+09:00 Subject: Re: Experimental PATCH to improve thread performance Charlie, Thank you for those references. I always understood why kill!/exit!/terminate! methods were doomed to be unsafe. However, now I see why even the "non-bang" versions of these methods and thread#raise are also unsafe. There is no practical way to interrupt a thread at any arbitrary point and expect it to preserve and/or repair data all invariants. Even so, I'm not convinced that Ruby's Thread.stop is unsafe, because, unlike Java's, Ruby's Thread.stop method can only act on the currently executing thread. One cannot stop another Ruby thread. A Ruby thread can only stop itself. Further, I believe that thread#raise (and even the non-bang variants exit, kill and terminate) can be made safe by adding an exception queue and an "interruptible" flag to each Ruby thread. This may provide Ruby thread safety without breaking most existing threaded applications. When a Ruby thread is interruptible, it acts upon exceptions raised in it by other threads. When it is not interruptible, exceptions raised by other threads are queued to be acted upon (in order received) when next the thread becomes interruptible. When a Ruby thread is first created, it would not be interruptible until it either explicitly sets its interruptible flag or blocks for any reason. A thread would always be interruptible while blocked. It would revert back to its previous interruptible status when it unblocked. A thread would always become uninterruptible whenever it acted upon an exception raised in it by Thread#raise. But, exceptions raised by Kernel.raise would not change the thread's interruptible status and they would always bypass the thread's exception queue. What this attempts to do is to turn Ruby's (very flexible) exception mechanism into a safe vehicle for messaging. Do you think there's any merit to this approach? Any obvious (or not to obvious) holes in it? - brent Charles Oliver Nutter-2 wrote: > > > See my description of threading in Ruby here: > > http://spec.ruby-doc.org/wiki/Ruby_Threading > > and JRuby here: > > http://spec.ruby-doc.org/wiki/JRuby_Threading > > ... snip ... > > Provably unsafe thread operations Ruby supports and should not: > > stop > exit/exit!/kill/kill!/terminate/terminate! > raise > global abort_on_exception > > Operations which are largely incompatible with concurrent threading > (which Ruby can't support if it wants to support concurrent threading > well): > > critical= > > I'll enumerate the reasons why these are unsafe if anyone wants to know. > I think Java's threading is a good model here; it provides the thread > operations that can be safely supported and does not provide the others. > > Here's an article that explains some of the reasons: > > http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html > > - Charlie > > > -- View this message in context: http://www.nabble.com/Experimental-PATCH-to-improve-thread-performance-tp14409855p14476198.html Sent from the ruby-core mailing list archive at Nabble.com.