Is there a specific implementation of `MutableMap`...
# getting-started
m
Is there a specific implementation of
MutableMap
that can be safely used when accessed from multiple `suspend fun`s? We’re seeing unpredictable behavior, where keys that should exist don’t.
ConcurrentHashMap
displays the same behavior as
mutableMapOf()
in our case.
j
ConcurrentHashMap
only makes its own operations atomic, but if your own business logic needs to have atomic sections, you might have to use a
Mutex
manually. Could you please share a piece of code reproducing your problem?
m
Not really, very far down in the weeds in customer’s code
c
It depends what your definition of "safely" is. If you write this:
Copy code
map[5] = someTransformation(map[5])
then you always have a risk two concurrent executions will corrupt each other, no matter the type of map you use. In these cases, the only thing you can do is use a Mutex everywhere
1