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

for a version that retries if there are not enough permits.


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

for a version that retries if there are not enough permits.