Mutex und Semaphore

This commit is contained in:
2022-09-29 13:45:06 +02:00
parent 4960ce061b
commit d3aa16361b
3 changed files with 76 additions and 0 deletions

14
Semaphore.kt Normal file
View File

@@ -0,0 +1,14 @@
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit
val semaphore = Semaphore(5, 5)
suspend fun main() {
semaphore.acquire() //reduces available permits, suspends when none available
semaphore.tryAcquire() //returns False if no permit available
semaphore.release() //releases permit, possible after acquire
semaphore.withPermit {
}
}