acquire

open fun TSemaphore.acquire()

Acquire 1 permit from a TSemaphore.

import arrow.fx.stm.TSemaphore
import arrow.fx.stm.atomically

suspend fun main() {
  //sampleStart
  val tsem = TSemaphore.new(5)
  atomically {
    tsem.acquire()
  }
  //sampleEnd
  println("Permits remaining ${atomically { tsem.available() }}")
}

This function will retry if there are no permits available.

See also

for a version that does not retry.


open fun TSemaphore.acquire(n: Int)

Acquire n permit from a TSemaphore.

import arrow.fx.stm.TSemaphore
import arrow.fx.stm.atomically

suspend fun main() {
  //sampleStart
  val tsem = TSemaphore.new(5)
  atomically {
    tsem.acquire(3)
  }
  //sampleEnd
  println("Permits remaining ${atomically { tsem.available() }}")
}

This function will retry if there are less than n permits available.

See also

for a version that does not retry.