How to use map/list/set in multi-threaded environm...
# kotlin-native
How to use map/list/set in multi-threaded environment? Currently mutation throws:
Copy code
Caused by: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlin.collections.HashMap@18af58
I tried making the reference atomic, but that doesn't change the reference of underlying hash table atomic so exception is still thrown, is there any built-in data-structure to use?
You can use atomic reference and immutable collections. To change, just copy it. If performance is a concern, you can consider persistent collections. Stately library may also help
👆 1
Well, I actually found something related to it: DetatchedObjectGraph
Though I'm not sure if there's any concise way to do this...
Welp, attaching and detaching for each access isn't that good... There's too much overhead.
I got something useful for my purpose by the way, atleast 35x faster than DetachedObjectGraph that by default use one bg thread for access for all the collections in it, and returns or insert frozen states. Kotlin/Native - Isolated State - DEV
Yeah, the other article in that series is about detached state, which isn't very useful:
👍 1