Commit a0178eea authored by Caitlin Matos's avatar Caitlin Matos

Imported Upstream version 1.0.0

parents
require 'thread'
# A synchronization barrier enables multiple threads to wait until all threads
# have all reached a particular point of execution before any thread
# continues.
class Barrier
# Initialize new barrier. The _count_ argument specifies the number of threads
# that must call #wait before any of them successfully return from the call.
# The value specified by _count_ must be greater than zero.
#
# For example
# b = Barrier.new(3)
# 3.times do
# Thread.new do
# print 1
# b.wait
# print 2
# end
# end
# sleep(1)
# puts
# produce
# 111222
def initialize(count)
@count_max = count
@count = 1
@mutex = Mutex.new
@lock = ConditionVariable.new
end
# The #wait function shall synchronize participating threads at the barrier.
# The calling thread shall block until the required number of threads have
# called #wait specifying the barrier.
def wait
@mutex.synchronize do
if @count < @count_max
@count += 1
@lock.wait(@mutex)
else
@count = 1
@lock.broadcast
end
end
nil
end
end
--- !ruby/object:Gem::Specification
name: barrier
version: !ruby/object:Gem::Version
version: 1.0.0
prerelease:
platform: ruby
authors:
- Aleksey Vereshchagin
autorequire:
bindir: bin
cert_chain: []
date: 2012-04-26 00:00:00.000000000 Z
dependencies: []
description: ! " A synchronization barrier enables multiple threads to wait until
all threads\n have all reached a particular point of execution before any thread\n
\ continues.\n"
email: alexeyv.90@gmail.com
executables: []
extensions: []
extra_rdoc_files: []
files:
- lib/barrier.rb
homepage: http://rubygems.org/gems/barrier
licenses: []
post_install_message:
rdoc_options: []
require_paths:
- lib
required_ruby_version: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: 1.8.6
required_rubygems_version: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
requirements: []
rubyforge_project:
rubygems_version: 1.8.11
signing_key:
specification_version: 3
summary: A barrier for threads synchronization
test_files: []
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment