3 Replies
      Latest reply on Dec 3, 2019 12:45 AM by eskimo
      robson.tekever Level 1 Level 1 (0 points)

        Hello,

         

        After updating to Xcode 10 I started having an issue where the app freezes when waiting for a semaphore. I tried changing to dispatch groups but the same thing happens: it works normally with Xcode 10 and freezes with Xcode 11 (both stable and beta).

         

        The code is basically this:

         

        result = false

        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

        dispatch_sync(dispatch_get_main_queue(), myBlock);

         

        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

        return result;

         

        Inside myBlock I display an AlertView that signals the semaphore when the uses presses either button.

        Simply changing to dispatch_async stops the freezing, but also breaks the functionality since I need the answer from the AlertView before returning.

        • Re: App freezes on Xcode11 when using dispatch group or semaphore
          eskimo Apple Staff Apple Staff (12,425 points)

          I’m confused by your code.  If you’re using a semaphore, why do you need to dispatch_sync?  The secondary thread will block on the semaphore until the main thread calls dispatch_semaphore_signal, so why does it need to call the main thread synchronously?

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

          ps DTS is closed 25…29 Nov in observance of the US Thanksgiving holiday.

            • Re: App freezes on Xcode11 when using dispatch group or semaphore
              robson.tekever Level 1 Level 1 (0 points)

              I was not the one who wrote the initial code, but from what I understand, the issue is that this method is originally called from another thread, several steps away:

               

              > dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{...

               

              And since what I need is to do is show an AlertView and wait for the user input, it needs to be done in the main thread, correct?

                • Re: App freezes on Xcode11 when using dispatch group or semaphore
                  eskimo Apple Staff Apple Staff (12,425 points)

                  And since what I need is to do is show an AlertView and wait for the user input, it needs to be done in the main thread, correct?

                  Right.  And you have to wait for the main thread to respond.  That all makes sense.  What doesn’t make sense is using two different ‘sync’ primitives to achieve the second goal.  Specifically:

                  • You call dispatch_sync, which runs the block on the main thread and doesn’t return until the block returns.

                  • You also use a semaphore to block the secondary thread until the alert completes.

                  If you have the latter, you don’t need the former.  You can dispatch_async to the main thread because you don’t need to wait until the block that posts the alert is finished.  Rather, you need to wait until the alert is done, and that’s what the semaphore does for you.


                  Notwithstanding the above, this technique is very concerning.  If any code on your main thread has a dependency on this secondary thread, or a resource held by the secondary thread, you run the risk of deadlock.

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"