//arrow-fx-stm/arrow.fx.stm/STM/tryAcquire

tryAcquire

common open fun TSemaphore.tryAcquire(): Boolean

Like TSemaphore.acquire except that it returns whether or not acquisition was successful.

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

suspend fun main() {
  //sampleStart
  val tsem = TSemaphore.new(0)
  val result = atomically {
    tsem.tryAcquire()
  }
  //sampleEnd
  println("Result $result")
  println("Permits remaining ${atomically { tsem.available() }}")
}

This function never retries.

See also

common

   
arrow.fx.stm.STM for a version that retries if there are not enough permits.

common open fun TSemaphore.tryAcquire(n: Int): Boolean

Like TSemaphore.acquire except that it returns whether or not acquisition was successful.

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

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

This function never retries.

See also

common

   
arrow.fx.stm.STM for a version that retries if there are not enough permits.

Do you like Arrow?

Arrow Org
<