I have a mutableMap and two functions the queue fu...
# getting-started
j
I have a mutableMap and two functions the queue function is getting called from different origins. But somehow the map is always empty if I modify it in these functions? Code:
Copy code
val buckets = mutableMapOf<String, RestClient.Bucket>()

suspend fun queue(endpoint: String, task: suspend () -> HttpResponse): HttpResponse {
     println(buckets.size) //always 0
     //....
     send(endpoint, task)
}

private suspend fun send(endpoint: String, task: suspend () -> HttpResponse): HttpResponse {
     buckets[endpoint] = bucket
     println(buckets.size) //always one
}
z
LinkedHashMap
is not safe to mutate from multiple threads without some form of synchronization
j
yes I also though something like that but I haven't found a working thread safe hashmap
(kotlin multiplatform)
z
Ah, yea i’m not aware of any multiplatform threadsafe implementations. You could use a mutex or something, not sure if there are multi-platform impls of that either. Another approach would be an immutable map with atomicfu.
s
not having threadsafe implementations in multiplatform might be due to the old memory manager in native?!? So maybe this changes soon-ish after the new memory manager goes live?? Kotlin 1.6, which might be release around the end of october, will at least contain a preview of that new memory manager. more on this see: https://blog.jetbrains.com/kotlin/2021/08/try-the-new-kotlin-native-memory-manager-development-preview/