:wave: Hello, new to Kotlin I’m trying to figure i...
# multiplatform
h
👋 Hello, new to Kotlin I’m trying to figure it out about support thread-safe singleton Where I can find more about:
private val shared: CustomSingleton by _lazy_(LazyThreadSafetyMode.SYNCHRONIZED) *{* CustomSingleton() *}*
I aim to access this singleton and his custom variable, which includes an array (read and write) from different threads. Or can you please share what I need to learn 🙂 I really appreciate any help you can provide.
j
I fear your question is a bit abstract - is it about the syntax of the statement you've shared? Or where did that "private val ..." came from - was it from an example that you try to mimic in your project? Maybe it would be better if you explain what you are trying to achieve in more concrete terms? 🙂
h
Copy code
class Singleton {

    private val array = ArrayDeque<String>()

    companion object {
        val shared: Singleton by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { Singleton() }
    }
    
    fun getElement(index: Int): String {
        return array[index]
    }
    
    fun add(item: String) {
        array.add(item)
    }
    
    fun size(): Int {
        return array.size
    }
}
The question is how to support multithreading. Is it safe to access the array from different threads?
p
You can use a coroutine mutex/semaphore. You can ask in #coroutines. Depending on your use case you may use some coroutine primitives as Channel or ShredFlow, StateFlow. Ultimately if you need isolation you can build your own Actor implementation. There is also an API for atomics which you can use to ensure single thread access. Like AtomicBoolean.
thank you color 1
j
@HenSh Is there a reason not to use
object Singleton { ... }
, without the
companion object { val shared ... }
thing? That should be thread safe according to https://stackoverflow.com/questions/30179793/are-kotlins-singletons-thread-safe
h
@Jan Holešovský thanks for your response, I found here https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-lazy-thread-safety-mode/ that: Locks are used to ensure that only a single thread can initialize the Lazy instance. But couldn’t find how to configure
object Singleton { ... }
without
companion object
So the last and most important case I need to understand if after the initialization of the singleton instance, the read and write from the array inside this instance are thread safe?
j
I don't think they are (https://stackoverflow.com/questions/37386116/are-kotlin-object-singletons-methods-synchronized); but also I think the \@Synchronized (suggested in the answer) is not implemented in Native (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-synchronized/). Please bear in mind though that I was dealing with all this 2-3 years ago, when the `object`s still had to be annotated with \@ThreadLocal not to cause InvalidMutabilityException, which is not the case any more with the new memory allocator since Kotlin 1.7.20. So I think you will need to synchronize depending on what you use to actually create the threads - if it is coroutines, I'd check what @Pablichjenkov said, otherwise I think you'll need to do with POSIX locking mechanism on iOS if you create threads the POSIX way on iOS... But back to your problem - it seems you are going though this exercise just to get a thread-safe array of strings - can't you use https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.concurrent/-atomic-array/ instead?
h
Thanks @Jan Holešovský I’ll try to play with it 🙂
👍 1