Using `atomicfu` , with the following block of cod...
# multiplatform
e
Using
atomicfu
, with the following block of code:
Copy code
private val lock = SynchronizedObject()
...

public actual override fun use(instance: T) {
  synchronized(lock) {
    doSomething(instance)
  }
}
What happens if
doSomething
is a suspendable function? Is this a use-case that I can handle somehow?
j
You could use
reentrantLock()
instead:
Copy code
val lock = reentrantLock()
lock.lock()
doSuspendSomething()
lock.unlock()
SynchronizedObject()
also supports manually calling
lock()
and
unlock()
too.
e
Thanks @Jeff Lockhart, I was mainly wondering if a suspension point in the middle of a lock could cause some kind of undefined behavior
j
There's nothing wrong with locking and unlocking the lock from different threads. A thread that is waiting on the lock will block until it's unlocked, so you'd just want to be sure there's not a lot of threads blocking waiting on the lock, otherwise it may make sense to use a suspending, rather than blocking, alternative, like
Mutex
.