What behavior is guaranteed in Grand Central Dispatch in Objective-C?

I think the best way to ask this question is with the code:

//Main method
for(int i = 0; i < 10; i++)
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         [self foo:i];
    });

}

- (void) foo: (int) i
{
    @synchronized(self)
    {
        NSLog(@"%d",i);
    }
}

In this case, is it guaranteed that the numbers 0-9 will be printed in order? Is there a chance that one of the threads waiting for the execution queue will be skipped? As a matter of fact. Does this really happen? What if I need the behavior above (we still use threads); how could i do that?

+5
source share
2 answers

In this case, is it guaranteed that the numbers 0-9 will be printed in order?

No.

Is there a chance that one of the threads waiting for the run queue will be skipped?

, "". " ?" ", ".

. ?

. concurrency , concurrency.

, , (- ); ?

, , . , , @synchronized() (, @synchronized() , , ).

+15

dispatch_get_global_queue

, , .

, , . , .

0

All Articles