From: Nobuyoshi Nakada Date: 2008-03-11T10:56:30+09:00 Subject: Re: [PATCH] timeouts for ConditionVariable#wait Hi, At Tue, 11 Mar 2008 03:58:07 +0900, MenTaLguY wrote in [ruby-core:15847]: > > 1. I added a second optional argument to ConditionVariable#wait which > > specifies a timeout in seconds. If no second argument is provided, > > #wait will wait indefinitely, as it did before. If a specified timeout > > elapses before the waiting thread is signaled, #wait raises > > TimeoutError. Timeouts <= 0 raise TimeoutError immediately. > > Since this first change seems to be uncontroversial, I've attached a > patch which implements this in 1.9 using the timeout argument of > Mutex#sleep. Mutex#sleep is not guaranteed that it exits in the specified time, it just sleeps the time with unlocking, and tries to achieve the lock again. If another thread takes the lock at that time, it sleeps forever. $ ruby19 -e ' m=Mutex.new Thread.start{sleep(1); m.synchronize{sleep(10)}} m.synchronize{p m.sleep(2)}' 11 So I don't think your implementation is very intuitive or intented. Timeout.timeout works properly in such case. $ ruby19 -rtimeout -e ' m=Mutex.new Thread.start{sleep(1); m.synchronize{sleep(10)}} m.synchronize{p((Timeout.timeout(3){m.sleep(2)} rescue $!))}' # -- Nobu Nakada