Hello, could someone tell me what is the differenc...
# announcements
m
Hello, could someone tell me what is the difference between these two ways of synchronization and why would I use one over the other? I noticed it's impossible to call suspending functions inside a synchronized block but it's fine with the other way, why is that?
h
They aren't the same, the first locks by calling
lock()
and
unlock()
on
lock
. The second holds a monitor on the
lock
object.
m
So what is the functional difference? Both seem to achieve the same thing?
g
same functionality. From documentation:
Memory semantic of the Mutex is similar to synchronized block on JVM
m
Thank you. But then why suspending function cannot be called inside a synchronized block and can inside a
withLock
?
g
Because
Mutex.withLock()
is suspend itself
1
m
Mystery solved. Thank you.
😉 1