https://kotlinlang.org logo
#getting-started
Title
# getting-started
j

Jan

09/10/2021, 7:59 PM
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

Zach Klippenstein (he/him) [MOD]

09/10/2021, 8:08 PM
LinkedHashMap
is not safe to mutate from multiple threads without some form of synchronization
j

Jan

09/10/2021, 8:12 PM
yes I also though something like that but I haven't found a working thread safe hashmap
(kotlin multiplatform)
z

Zach Klippenstein (he/him) [MOD]

09/10/2021, 9:38 PM
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

Stephan Schroeder

09/11/2021, 7:22 PM
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/
6 Views