title: How create thread safe variable in Swift 3?
body: <p>I have singleton class </p>
  
  <pre><code>class DeviceController:NSObject, CocoaMQTTDelegate {
       static let sharedInstance = DeviceController()
       var deviceOnArray:[String] = []
       var deviceOffArray:[String] = []
       private override init() {
  
          clientID = "xyz-" + String(ProcessInfo().processIdentifier)
          mqtt = CocoaMQTT(clientID: clientID, host: "device_controller.xyz.net", port: 1883)
          mqtt.username = "username"
          mqtt.password = "password"
          mqtt.willMessage = CocoaMQTTWill(topic: "/will", message: "dieout")
          mqtt.keepAlive = 30
          mqtt.cleanSession = true
          DeviceController.isConnecting = true
          super.init()
          mqtt.delegate = self
          mqtt.connect()
          self.registerBackgroundTask()
      }
     func sendArm(topic:String){
        // add device to deviceOnArray
     }
     func sendDisarm(topic:String){
        // remove device from deviceOnArray if exist here if I check by code that device is in array it returns false but on console if print it contains the device, It only heppens when I call sendArm and sendDisarm with a second.
       let lockQueue = DispatchQueue(label: "com.test.LockQueue")
          lockQueue.sync() {
             // all code now inside this
           }
       // I also used above code but it's not working
     }
  
  
  }
  </code></pre>
  
  <p>If you read the code then you'll know that I am facing problem in reading correct value from deviceOnArray/deviceOffArray, I am not sure how to explain this problem but I think what I need here is Obj-C atomic thread safe variable. Any idea how create one ?</p>