Jeevan Deep Singh
10/29/2024, 11:33 AMDmitry Khalanskiy [JB]
10/29/2024, 11:41 AMis there a way to wait for a mutex to unlock ?Nope, this operation would be completely meaningless.
if (mutex.isUnlocked) {
doSomething()
}
is susceptible to this issue:
• Thread A checks mutex.isUnlocked
. It receives true
.
• Thread B locks the mutex.
• Thread B enters the section protected by the mutex and starts mutating the data.
• Thread A calls doSomething
.Jeevan Deep Singh
10/29/2024, 11:43 AMkevin.cianfarini
10/29/2024, 11:45 AMDmitry Khalanskiy [JB]
10/29/2024, 12:00 PMMutableStateFlow
can represent some state changes to which should notify several consumers (and accessing .value
doesn't need to be wrapped in a lock!). This is often a good solution to the problem of having multiple consumers.Jeevan Deep Singh
10/29/2024, 12:04 PMMutableSharedFlow
instead, where I could use .first()
which would be suspended until the
write method emits the value, and the subsequent calls to the get method can get the values without the need to acquire the lockDmitry Khalanskiy [JB]
10/29/2024, 12:06 PMreplay
parameter to 1
.