Class

Mutex

Inheritance
< Object

Mutex implements a simple semaphore that can be used to coordinate access to shared data from multiple concurrent threads.

Example:

  require 'thread'
  semaphore = Mutex.new

  a = Thread.new {
    semaphore.synchronize {
      # access shared resource
    }
  }

  b = Thread.new {
    semaphore.synchronize {
      # access shared resource
    }
  }

Methods

Class

Visibility Signature
public new ()

Instance

Visibility Signature
public exclusive_unlock () {|| ...}
public lock ()
public locked? ()
public synchronize () {|| ...}
public try_lock ()
public unlock ()

Class Method Detail

new()

Creates a new Mutex

Instance Method Detail

exclusive_unlock() {|| ...}

If the mutex is locked, unlocks the mutex, wakes one waiting thread, and yields in a critical section.

lock()

Attempts to grab the lock and waits if it isn‘t available.

locked?()

Returns true if this lock is currently held by some thread.

synchronize() {|| ...}

Obtains a lock, runs the block, and releases the lock when the block completes. See the example under Mutex.

try_lock()

Attempts to obtain the lock and returns immediately. Returns true if the lock was granted.

unlock()

Releases the lock. Returns nil if ref wasn‘t locked.