https://kotlinlang.org logo
#coroutines
Title
# coroutines
l

Laxystem

03/05/2024, 7:35 AM
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

Sam

03/05/2024, 8:32 AM
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

CLOVIS

03/05/2024, 9:03 AM
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

Laxystem

03/05/2024, 9:39 AM
Specific thing being adding something to a map if it is missing.
c

CLOVIS

03/05/2024, 9:54 AM
That's still too precise, can you describe what you're doing at a higher level? What is the map used for?
l

Laxystem

03/05/2024, 10:46 AM
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