Locking an object from being accessed by multiple threads - Objective-C

后端 未结 3 1584
时光说笑
时光说笑 2020-12-12 17:40

I have a question regarding thread safety in Objective-C. I\'ve read a couple of other answers, some of the Apple documentation, and still have some doubts regarding this, s

3条回答
  •  情话喂你
    2020-12-12 17:44

    As in Swift 3 in WWDC 2016 Session Session 720 Concurrent Programming With GCD in Swift 3, you should use queue

    class MyObject {
      private let internalState: Int
      private let internalQueue: DispatchQueue
    
      var state: Int {
        get {
          return internalQueue.sync { internalState }
        }
    
        set (newValue) {
          internalQueue.sync { internalState = newValue }
        }
      }
    }
    

提交回复
热议问题