Thread safe singleton in swift

后端 未结 1 447
走了就别回头了
走了就别回头了 2021-02-01 23:47

I have and Application which has a singleton that stores information across the whole app. However, this is creating some data race issues when using the single

相关标签:
1条回答
  • 2021-02-02 00:14

    Thanks to @rmaddy comments which pointed me in the right direction I was able to solve the problem.

    In order to make the property foo of the Singleton thread safe, it need to be modified as follows:

        class Singleton {
    
        static let shared = Singleton()
    
        private init(){}
    
        private let internalQueue = DispatchQueue(label: "com.singletioninternal.queue",
                                                  qos: .default,
                                                  attributes: .concurrent)
    
        private var _foo: String = "aaa"
    
        var foo: String {
            get {
                return internalQueue.sync {
                    _foo
                }
            }
            set (newState) {
                internalQueue.async(flags: .barrier) {
                    self._foo = newState
                }
            }
        }
    
        func setup(string: String) {
            foo = string
        }
    }
    

    Thread safety is accomplished by having a computed property foo which uses an internalQueue to access the "real" _foo property.

    Also, in order to have better performance internalQueue is created as concurrent. And it means that it is needed to add the barrier flag when writing to the property.

    What the barrier flag does is to ensure that the work item will be executed when all previously scheduled work items on the queue have finished.

    0 讨论(0)
提交回复
热议问题