nikhil kumar
03/21/2022, 8:26 AMmagnumrocha
03/21/2022, 9:07 AMmagnumrocha
03/21/2022, 9:08 AMsynchronized
or ReentrantLock
for that. Coroutine’s alternative is called Mutex. It has lock and unlock functions to delimit a critical section. The key difference is that Mutex.lock()
is a suspending function. It does not block a thread.”kpgalligan
03/21/2022, 3:19 PMkpgalligan
03/21/2022, 3:20 PMfun <R> synchronized(lock: Lockable, block: () -> R): R
. Lockable
is an expect class expect open class Lockable()
. See source: https://github.com/InsertKoinIO/koin/blob/3.2.x/core/koin-core/src/commonMain/kotlin/org/koin/mp/KoinPlatformTools.ktkpgalligan
03/21/2022, 3:21 PMLockable
is typealiased to Any
, because any class in the JVM can be use for synchronize. actual typealias Lockable = Any
. Source: https://github.com/InsertKoinIO/koin/blob/3.2.x/core/koin-core/src/jvmMain/kotlin/org/koin/mp/PlatformToolsJVM.ktkpgalligan
03/21/2022, 3:22 PMLockable
is a class you need to extend, but it has a lock internally which you can use like synchronize
on the JVM.
actual open class Lockable {
internal val lock = Lock()
}
kpgalligan
03/21/2022, 3:22 PMTijl
03/21/2022, 4:13 PM