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.new a = Thread.new { semaphore.synchronize { # access shared resource } } b = Thread.new { semaphore.synchronize { # access shared resource } }
Public Instance Methods
() → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/thread.rbs, line 1530
def lock: () -> self
Attempts to grab the lock and waits if it isnโt available. Raises ThreadError if mutex was locked by the current thread.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/thread.rbs, line 1538
def locked?: () -> bool
Returns true if this lock is currently held by some thread.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/thread.rbs, line 1546
def owned?: () -> bool
Returns true if this lock is currently held by current thread.
[X] () { () → X } → X
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/thread.rbs, line 1555
def synchronize: [X] () { () -> X } -> X
Obtains a lock, runs the block, and releases the lock when the block completes. See the example under Thread::Mutex.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/thread.rbs, line 1564
def try_lock: () -> bool
Attempts to obtain the lock and returns immediately. Returns true if the lock was granted.
() → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/thread.rbs, line 1573
def unlock: () -> self
Attempts to grab the lock and waits if it isnโt available. Raises ThreadError if mutex was locked by the current thread.