0

Why the code if (dispatch_get_current_queue() == socketQueue) is needed? why can't we just use dispatch_sync(socketQueue, block) directly???

Thanks in advance!

- (BOOL)isConnected
{
__block BOOL result = NO;

dispatch_block_t block = ^{
    result = (flags & kConnected) ? YES : NO;
};

if (dispatch_get_current_queue() == socketQueue)
    block();
else
    dispatch_sync(socketQueue, block);

return result;
}

BTW, the code is from XMPPFramework

Rob
  • 415,655
  • 72
  • 787
  • 1,044
Henry
  • 481
  • 4
  • 17

1 Answers1

2

You cannot call dispatch_sync to schedule blocks on the current serial queue since this will deadlock. Dispatch_sync waits until the block finished executing, but it will never start to run before the current block finished running.

Sven
  • 22,475
  • 4
  • 52
  • 71
  • Hi Sven, see you again. a little bit unclear. What about dispatch_async, if the block changed a __block value, how could it be async??? – Henry Jul 03 '12 at 07:18