2 Replies
      Latest reply: Jan 13, 2018 6:47 PM by cccJJJJ RSS
      cccJJJJ Level 1 Level 1 (0 points)

        The first situation is that I create a Command Line Tool Application,and run this code.


        NSLog(@"Main:%@", [NSThread currentThread]);
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_group_t group = dispatch_group_create();
        dispatch_group_async(group, queue, ^{ NSLog(@"Task1:%@", [NSThread currentThread]); });
        dispatch_group_async(group, queue, ^{ NSLog(@"Task2:%@", [NSThread currentThread]); });
        dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"Finish:%@", [NSThread currentThread]); });


        The log in terminal is

        Main:<NSThread: 0x1028033b0>{number = 1, name = main}
        Task2:<NSThread: 0x10040f0f0>{number = 2, name = (null)}
        Task1:<NSThread: 0x1006008d0>{number = 3, name = (null)}


        If I want to show last log in 'queue' and replace the 'main queue'


        dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"Finish:%@", [NSThread currentThread]); });

        with 'queue' :


        dispatch_group_notify(group, queue, ^{ NSLog(@"Finish:%@", [NSThread currentThread]); });


        The terminal print the last log.But why it can't revoke in Main queue?


        When i copy this code to simple iOS Application(revoke block in main queue).All works well:

        Main:<NSThread: 0x600000070ac0>{number = 1, name = main}
        Task2:<NSThread: 0x6000002633c0>{number = 3, name = (null)}
        Task1:<NSThread: 0x600000263480>{number = 4, name = (null)}
        Finish:<NSThread: 0x600000070ac0>{number = 1, name = main}


        And I try to add  sleep(1) over Task1 in 'Command Tool Line', but it seems block the queue and only print log:Task2.... But this all works well in simple iOS Application.

        Why lead to these different?

        • Re: Why Dispatch_group_notify works different in different environment?
          QuinceyMorris Level 8 Level 8 (5,170 points)

          In the command line tool version (your first 10 line code sample), is that the whole of the main() function? In that case, my guess is that your process exits before the notification is executed. In the iOS version, if it takes time for the app to quit, there may be time for the notification to execute before termination.

            • Re: Why Dispatch_group_notify works different in different environment?
              cccJJJJ Level 1 Level 1 (0 points)


              You seems right and i think the different maybe related to Runloop:

              When I add


              to bottom of line 11.The notify revoked in Main Thread correctly.


              I read the libdispatch ,this function reboot the root queue. In the first case,i think the main queue was disposed after dispatch_notify.... so program can't find target queue(Main queue) to execute the NSLog Block when notify invoked.

              With the runloop in iOS Application the main queue will exist all the time