What's the best way to only have a single coroutin...
# coroutines
l
What's the best way to only have a single coroutine execute a piece of code (that works on both Kotlin/Wasm and Kotlin/JVM)? Currently using a single-threaded coroutine dispatcher with `async`/`await`. Also know of
synchronized
but afaik it doesn't really work?
I have a suspicion my current approach won't work - i.e. if a coroutine on the single-threaded dispatcher is suspended, it'll execute another one.
s
Can you share some more about the problem you're trying to solve? I'm not sure exactly what you mean by "only have a single coroutine execute a piece of code".
c
The literal solution would be to use a
Mutex
, but we would need more information to assess whether it's the correct solution.
👍 3
l
Specific thing being adding something to a map if it is missing.
c
That's still too precise, can you describe what you're doing at a higher level? What is the map used for?
l
For providers, objects that take an ID - a URI - and return an object (from the database in this case)
Each object uses a common interface to parse the URIs and to create new ones. Thing is some providers need to use other providers, so there's a what the map is for.
Ended up just making the thing immutable