Is there any cleanup you need to do after using a mutex and withLock? Am I correct in guessing that withLock is doing all of that (eg, unlocking)
k
kevin.cianfarini
01/18/2024, 7:03 PM
The implementation is essentially
Copy code
public suspend inline fun <T> Mutex.withLock(owner: Any? = null, action: () -> T): T {
lock(owner)
return try {
action()
} finally {
unlock(owner)
}
}
So yes, cleanup will happen for you.
g
Garret Yoder
01/18/2024, 7:04 PM
Got it. Is there any way you can have a mutex with multiple locks in a pool? (eg; allow 2 concurrent coroutines or n number, etc) or would you need to have N mutexes and try locking until you found a free one?
k
kevin.cianfarini
01/18/2024, 7:08 PM
You should probably use a Semaphor
g
Garret Yoder
01/18/2024, 7:13 PM
Oh cool I didn't know that existed, thanks thats pretty cool it looks like the usage is the same with withPermit