//arrow-fx-stm/arrow.fx.stm/STM/acquire

acquire

common 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

common

   
arrow.fx.stm.STM for a version that does not retry.

common 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

common

   
arrow.fx.stm.STM for a version that does not retry.

Do you like Arrow?

Arrow Org
<