class Sync::Mutex

Overview

A mutual exclusion lock to protect critical sections.

A single fiber can acquire the lock at a time. No other fiber can acquire the lock while a fiber holds it.

This lock can for example be used to protect the access to some resources, with the guarantee that only one section of code can ever read, write or mutate said resources.

NOTE Consider Exclusive(T) to protect a value T with a Mutex.

Included Modules

Defined in:

mutex.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(type : Type = :checked) #

[View source]

Instance Method Detail

def lock : Nil #

Acquires the exclusive lock.


[View source]
def synchronize(& : -> U) : U forall U #

Acquires the exclusive lock for the duration of the block. The lock will be released automatically before returning, or if the block raises an exception.


[View source]
def unlock : Nil #

Releases the exclusive lock.


[View source]