https://kotlinlang.org logo
Title
g

Gta

12/04/2018, 2:57 PM
is there an easy way to synchronize a suspend function ?
a

andi

12/04/2018, 3:18 PM
Hi, synchronize in a way so that the function is only executed once at every given point in time? Check this out and the other sections: https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/shared-mutable-state-and-concurrency.md#actors We use actors for such cases for now
k

kevinherron

12/04/2018, 3:28 PM
there's a coroutines aware`Mutex`:
val mutex = Mutex()
suspend fun foo() = mutex.withLock { ... }
g

gildor

12/04/2018, 4:01 PM
What is your use case? Usually better (easier to use, less error-prone) to use actor for that
g

Gta

12/06/2018, 1:51 PM
My usecase was exactly like @andi said but I used Mutex. I will checkout actor then 😉 Thx guys.
k

kevinherron

12/07/2018, 12:51 AM
actor is overkill if mutex will suffice. actor is also deprecated/experimental API. (don't get me wrong, i really like actor, especially what the new typed actor stuff is shaping up to be, but it's overkill for some cases)