Skip to content

Instantly share code, notes, and snippets.

@halorgium
Last active December 17, 2015 07:19
Show Gist options
  • Save halorgium/b7689c261ec91c462f80 to your computer and use it in GitHub Desktop.
Save halorgium/b7689c261ec91c462f80 to your computer and use it in GitHub Desktop.
distributed lock aka ConditionVariable
# this requires the master branch of Celluloid
require 'celluloid'
class Worker
include Celluloid
def initialize(condition, number)
@condition = condition
@number = number
async.work
end
def work
Celluloid::Logger.info "worker #{@number.inspect} is waiting"
@condition.wait
Celluloid::Logger.info "worker #{@number.inspect} is working"
sleep 1
Celluloid::Logger.info "worker #{@number.inspect} is finished"
ensure
Celluloid::Logger.info "worker #{@number.inspect} is signalling"
@condition.signal
async.work
end
end
condition = Celluloid::Condition.new
10.times do |number|
Worker.new(condition, number)
end
# start the work
condition.signal
sleep
source "https://rubygems.org"
gem "celluloid", github: "celluloid/celluloid"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment