Skip to content
On this page

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class · Ruby 3.0

Mutex

< Object 7 instance methods 1 class methods

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

Example:

Example
semaphore = Mutex.new

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

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

Class methods 1

Instance methods 7

Ruby version changes 1

  1. 3.0
    • Mutex is now acquired per-Fiber instead of per-Thread. This change should be compatible for essentially all usages and avoids blocking when using a scheduler. [Feature #16792]