title: How to use multithread in Swift
body: <p>I have two tasks : task1 and task2. I want to execute task2 after task1 finishes.</p>
  
  <pre><code>let globalQueueDefault = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
  
  dispatch_sync(globalQueueDefault){
  
  self.activityIndicatorView.hidden = false
  self.activityIndicatorView.startAnimating()
  
      task1()
  
      sleep(6)
      dispatch_sync(globalQueueDefault) { () -&gt; Void in
  
          task2()                                             
      }
  }
  </code></pre>
  
  <p>I searched in internet, I find <code>NSLock</code>,<code>NSConditionLock</code> and <code>objc_sync_enter</code>...I have try them, but it doesn't work...</p>
  
  <pre><code>let lock = NSLock()
  let globalQueueDefault = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
  
  dispatch_sync(globalQueueDefault){
  
  self.activityIndicatorView.hidden = false
  
  self.activityIndicatorView.startAnimating() 
  
  self.lock.lock()
  
  task1()
  self.lock.unlock()
  
  sleep(6)
  dispatch_sync(globalQueueDefault) { () -&gt; Void in
  self.lock.lock()
      task2() 
  self.lock.unlock()                                            
  }
  }
  </code></pre>
  
  <p>I also tried <code>NSConditionLock</code> and <code>objc_sync_enter</code>...It doesn't work. How I can use <code>lock</code> in swift ? Could you give me a example base on my code? Thank you.</p>
  
  <p>PS: I don't want to use <code>callback</code> here...because I have tried it, I think multithread is more closer to my answer, Thank you.</p>