mbonnin
10/15/2023, 11:57 AMpthread_cond_wait
like there is Mutex
for pthread_mutex_t
?
Have multiple coroutines suspend on a condition and resume all of them (one at a time) when it becomes true? Or am I thinking wrong?Jeff Lockhart
10/15/2023, 1:03 PMmbonnin
10/15/2023, 3:05 PMvar enabled: Boolean = false
var lock = ReentrantLock()
suspend fun backgroundTask() {
while (true) {
delay(Random.nextLong(1000))
lock.withLock {
enabled = !enabled
}
}
}
// Stuff that wants to interact with backgroundTask only when it's enabled
// May be be called any number of times concurrently
suspend fun anotherCoroutine() {
lock.withLock {
if (enabled) {
doStuff()
} else {
// How I wait here?
}
}
}
Jeff Lockhart
10/15/2023, 5:34 PMsimon.vergauwen
10/15/2023, 5:47 PMbezrukov
10/15/2023, 7:04 PMenabled.first { it }
Jeff Lockhart
10/15/2023, 7:35 PMCountDownLatch
and CyclicBarrier
implementations in Kotlin, if you need. ☝🏼