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

tryAcquire

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

TSemaphore.acquire

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

TSemaphore.acquire

Do you like Arrow?

Arrow Org
<