Search code examples
swiftgrand-central-dispatchswift3

Synchronize Properties in Swift 3 using GCD


I watched this years WWDC GCD talk lately and I think there is a code snippet something is wrong with. It is about making a property thread-safe using DispatchQueues.

class MyObject {
    private var internalState: Int
    private let internalQueue: DispatchQueue // Serial or Concurrent?

    var state: Int {
        get {
            return internalQueue.sync { internalState }
        }

        set (newState) {
            internalQueue.sync { internalState = newState }
        }
    }
}

They use a DispatchQueue to lock a property. But i think this snippet is not valid, because the internalQueue could be concurrent. So if we call the setter from two different DispatchQueues/Threads if that internal queue is not serial, it could also lead to threading problems right? Because in my understanding sync just holds the invoking thread and continues if the task is complete. What do you think about this snippet? Am I wrong?


Solution

  • But i think this snippet is not valid, because the internalQueue could be concurrent

    But it isn't concurrent. Dispatch queues that you create are serial by default. That is the point of the technique (and the example).