From: Eric Hodel Date: 2006-01-27T04:30:07+09:00 Subject: Re: threading sync On Jan 25, 2006, at 5:28 AM, Erik Veenstra wrote: >> My question is that do I really need mutex? > > No, you don't. > > You could as well use Thread.exclusive. This prevents other, > excisting threads from being run. If the given block can't get > blocked (e.g. by IO), it's much faster then Mutex. No, Thread.exclusive (which uses Thread.critical) does not prevent other threads from being run. It prevents them from being scheduled automatically. You can still switch threads manually while Thread.critical is true. $ cat exclusive.rb require 'thread' Thread.exclusive do Thread.new { sleep 10 } puts 'should be instant' end $ time ruby exclusive.rb should be instant real 0m10.061s user 0m0.016s sys 0m0.013s $ cat mutex.rb require 'thread' m = Mutex.new m.synchronize do Thread.new do m.synchronize do sleep 10 end end puts 'should be instant' end $ time ruby mutex.rb should be instant real 0m0.060s user 0m0.016s sys 0m0.014s -- Eric Hodel - drbrain@segment7.net - http://segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com