class Concurrent::Semaphore

# 1
# 0
# 1
# prints:
puts semaphore.available_permits
end
puts semaphore.available_permits
semaphore.acquire do
puts semaphore.available_permits
semaphore = Concurrent::Semaphore.new(1)
@example
# Thread 1 acquired semaphore
# Thread 4 releasing semaphore
# Thread 2 acquired semaphore
# Thread 3 acquired semaphore
# prints:
[t1, t2, t3, t4].each(&:join)
end
semaphore.release
puts “Thread 4 releasing semaphore”
sleep(2)
t4 = Thread.new do
end
puts “Thread 3 acquired semaphore”
semaphore.acquire
t3 = Thread.new do
end
puts “Thread 2 acquired semaphore”
semaphore.acquire
t2 = Thread.new do
end
puts “Thread 1 acquired semaphore”
semaphore.acquire
t1 = Thread.new do
semaphore = Concurrent::Semaphore.new(2)
@example
@!macro semaphore_public_api
released after the block finishes executing.
Alternatively, permits may be acquired within a block, and automatically
count of the number available and acts accordingly.
However, no actual permit objects are used; the Semaphore just keeps a
releasing a blocking acquirer.
available, and then takes it. Each {#release} adds a permit, potentially
permits. Each {#acquire} blocks if necessary until a permit is
A counting semaphore. Conceptually, a semaphore maintains a set of
@!macro semaphore