title: How to handle crashes caused by semaphore in iOS？
body: <p>Crashes when the class is destructed, how to handle unfinished semaphore?</p>
  <pre><code>class CrashTestViewCtrl: UIViewController {
  
      private var semaphore = DispatchSemaphore(value: 2)
  
      override func viewDidLoad() {
          super.viewDidLoad()
          
          DispatchQueue.global().async { [weak self] in
              self?.semaphore.wait()
              // do something ......
          }
      }
      
      deinit {
          print(&quot;……deinit……&quot;)
      }
  }
  </code></pre>
  <p><img src="https://i.stack.imgur.com/gVAs1.png" alt="EXC_BAD_INSTRUCTION" /></p>