ri > Concurrent::Semaphore

= Concurrent::Semaphore < SemaphoreImplementation

(from /home/chedong/.local/share/rdoc)
------------------------------------------------------------------------
@!macro semaphore

  A counting semaphore. Conceptually, a semaphore maintains a set of
  permits. Each {#acquire} blocks if necessary until a permit is
  available, and then takes it. Each {#release} adds a permit, potentially
  releasing a blocking acquirer.
  However, no actual permit objects are used; the Semaphore just keeps a
  count of the number available and acts accordingly.

@!macro semaphore_public_api @example
  semaphore = Concurrent::Semaphore.new(2)

  t1 = Thread.new do
    semaphore.acquire
    puts "Thread 1 acquired semaphore"
  end

  t2 = Thread.new do
    semaphore.acquire
    puts "Thread 2 acquired semaphore"
  end

  t3 = Thread.new do
    semaphore.acquire
    puts "Thread 3 acquired semaphore"
  end

  t4 = Thread.new do
    sleep(2)
    puts "Thread 4 releasing semaphore"
    semaphore.release
  end

  [t1, t2, t3, t4].each(&:join)

  # prints:
  # Thread 3 acquired semaphore
  # Thread 2 acquired semaphore
  # Thread 4 releasing semaphore
  # Thread 1 acquired semaphore
------------------------------------------------------------------------

Generated by phpman v4.9.26-1-g511901d · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-29 03:20 @216.73.217.46
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!