Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created March 13, 2026 17:57
Show Gist options
  • Select an option

  • Save havenwood/ffdd14ffe299eecfa9e2438a4acae685 to your computer and use it in GitHub Desktop.

Select an option

Save havenwood/ffdd14ffe299eecfa9e2438a4acae685 to your computer and use it in GitHub Desktop.
An example of double-checked locking with a Mutex for safe concurrent access
module Example
ONCE = Once.new
def self.once
ONCE.call do
# work
end
end
end
class Once
def initialize
@mutex = Mutex.new
@done = false
end
def done? = @done
def call
return if @done
@mutex.synchronize do
return if @done
yield
@done = true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment