I have a simple example of using select{} on mutex...
# announcements
p
I have a simple example of using select{} on mutex.onLock: https://gist.github.com/pambrose/4eb82e623161ee7758551ad6a9649ab6 Based on the docs and the name “onLock” I am expecting the select to take place when an unlocked mutex is locked. However, I am seeing the opposite behavior, i.e., if I use locked mutexes, I am seeing the select take place when the mutex is unlocked. The docs for Mutex.onLock say that it “selects when the mutex is locked.” Given the behavior I am seeing, I would expect the name to be “onUnlocked”. I would appreciate someone setting me straight on this.
e
onLock
is not a listener when called the mutex was locked by something.
onLock
is a select clause that instructs select expression to attempt acquiring a lock and call the corresponding block of code when this attempt succeeds.
#coroutines if a better channel for those kind of questions
☝️ 1
So, when you
unlock()
a locked mutex, then
onLock
select clause succeeds and you get the corresponding message.
p
Sorry for not posting this to #coroutines. The name is what threw me, but I can see the need to keep Mutex.onLock consistent with Mutex.lock and Mutex.tryLock. I will put in a PR to change the Mutex.onLock docs to:
Copy code
Clause for [select] expression of [lock] suspending function that selects when the mutex is unlocked.
Thanks for your answer. I really appreciate it.
👍 1
e
This would be a wrong doc, though, either. It is better to be more explicit about the fact that it selects when we can lock unlocked lock
p
Got it. Thanks. I am hugely appreciative for what you have done with coroutines and Kotlin. I have not not enjoyed programming this much in many years.