Skip to content

Instantly share code, notes, and snippets.

@krdlab
Created August 13, 2018 02:01
Show Gist options
  • Save krdlab/6e6d425efe756b10d12cb39a1fe7e37d to your computer and use it in GitHub Desktop.
Save krdlab/6e6d425efe756b10d12cb39a1fe7e37d to your computer and use it in GitHub Desktop.
# encoding: utf-8
class CyclicBarrier
def initialize(n)
@n = n
@await_count = 0
@mutex = Mutex.new
@var = ConditionVariable.new
end
def await
@mutex.synchronize do
@await_count += 1
if @await_count == @n then
@var.broadcast
else
@var.wait(@mutex)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment