How to make suspend function synchronized (thread-...
# coroutines
r
How to make suspend function synchronized (thread-safe)?
t
mutex.withLock {}
r
I saw that was wondering if there is a better way
thanks
t
i have used mutex in many situations and it works "A treat" 😉
👍 1
🤝 1
r
if I were to use
Mutex
should the object be created inside the method or inside the class holding that method?
sorry for the noob question but I haven’t use it before :d
g
Could you show some example what you want to synchronize?
r
suspend fun foo(){}
I want this function to be synchronized
does that make sense?
u
field, you need to share the mutex instance in order to do its thing
👍 1
e
what are you trying to protect from concurrency, there may be a better solution than synchronization
☝️ 1
synchronized
functions are often a poor choice even in Java
☝️ 1
g
yes, my question was about what you try to do concurrently
t
in my case i have a "Manager" class that i employ to create and observe all my WorkManager background workers i use mutex.withLock{} to ensure my worker chains are created, scheduled and observed in a "controlled" manner as my users can trigger multiple worker chains concurrently