Hello, can anyone have some practical solutions to...
# kotlin-native
q
Hello, can anyone have some practical solutions to access & modify
shared singleton
between threads like iOS native
sharedInstance
behaviour ? I have read [CONCURRENCY.md](https://github.com/JetBrains/kotlin-native/blob/master/CONCURRENCY.md) and [IMMUTABILITY.md](https://github.com/JetBrains/kotlin-native/blob/master/IMMUTABILITY.md). However
@threadLocal
annotation can not share states between threads, but
shared instance
in iOS domain means it shared states in all threads.
k
State visible by multiple threads will be frozen. If you need to change it you need to use atomics
Well, will a copy-on-write with frozen state perform worse than NSMutableArray? I would assume yes. NSMutableArray is, as far as I know, not thread safe, so I’m not sure how that would avoid data issues if multiple threads are accessing it. https://stackoverflow.com/a/12098115/227313
However, regardless of what happens on iOS, the Kotlin Native state rules will enforce certain things. I can tell you for sure Stately’s collections were not designed with performance in mind, and will have a review and possibly a new implementation at some point in the next few months. However, they’ll work. There’s also a linked list if COW list isn’t what you’re looking for.
For performance, I’d probably have a list local to a thread and push/pull with a worker, but I have no idea what you’re trying to build.
q
For this solution “For performance, I’d probably have a list local to a thread and push/pull with a worker”, does
worker
support sync pull mechanism like
dispatch_sync
?
Thanks for your detailed explanation. Actually, the performance is just my consideration in advance and we have not encountered concrete performance issue in our product now.
o