class Thread::Mutex
Thread::Mutex implements a simple semaphore that can be used to coordinate access to shared data from multiple concurrent threads.
Example:
semaphore =Thread::Mutex.newa =Thread.new {semaphore.synchronize {# access shared resource }}b =Thread.new {semaphore.synchronize {# access shared resource }}
Public Class Methods
Public Instance Methods
Source
# File thread_sync.rb, line 100deflockPrimitive.rb_mut_lockend
Attempts to grab the lock and waits if it isn’t available. RaisesThreadError ifmutex was locked by the current thread.
Source
# File thread_sync.rb, line 83deflocked?Primitive.cexpr!%q{ RBOOL(mutex_locked_p(mutex_ptr(self))) }end
Returnstrue if this lock is currently held by some thread.
Source
# File thread_sync.rb, line 91defowned?Primitive.rb_mut_owned_pend
Returnstrue if this lock is currently held by current thread.
Source
# File thread_sync.rb, line 147defsleep(timeout =nil)Primitive.rb_mut_sleep(timeout)end
Releases the lock and sleepstimeout seconds if it is given and non-nil or forever. RaisesThreadError ifmutex wasn’t locked by the current thread.
When the thread is next woken up, it will attempt to reacquire the lock.
Note that this method can wakeup without explicitThread#wakeup call. For example, receiving signal and so on.
Returns the slept time in seconds if woken up, ornil if timed out.
Source
# File thread_sync.rb, line 127defsynchronizeraiseThreadError,"must be called with a block"unlessdefined?(yield)Primitive.rb_mut_synchronizeend
Obtains a lock, runs the block, and releases the lock when the block completes. See the example underThread::Mutex.
Source
# File thread_sync.rb, line 109deftry_lockPrimitive.rb_mut_trylockend
Attempts to obtain the lock and returns immediately. Returnstrue if the lock was granted.
Source
# File thread_sync.rb, line 118defunlockPrimitive.rb_mut_unlockend
Attempts to grab the lock and waits if it isn’t available. RaisesThreadError ifmutex was locked by the current thread.