Class Mutex
In: lib/thread.rb
Parent: 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

Public Class methods

Creates a new Mutex

Public Instance methods

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

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

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

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

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

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

[Validate]

ruby-doc.org is hosted and run by James Britt and Happy Camper Studios, a Ruby application development company in Phoenix, Arizona. Ruby-doc.org was created in 2002 to promote the Ruby language and to help other Ruby hackers.

Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.

For more information on the Ruby programming language, visit ruby-lang.org.

Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.