Skip to content

Instantly share code, notes, and snippets.

@farktronix
Last active August 29, 2015 14:20
Show Gist options
  • Save farktronix/da8f3c0ce01a5311364d to your computer and use it in GitHub Desktop.
Save farktronix/da8f3c0ce01a5311364d to your computer and use it in GitHub Desktop.
Dispatch to a serial queue without deadlocking
static const char *kMyQueueSentinel = "🐔";
- (instancetype)init {
...
_serialQueue = dispatch_queue_crete("com.my.serialqueue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_set_specific(self.serialQueue, kMyQueueSentinel, (void *)1, NULL);
...
}
static inline void _performOnSerialQueue(dispatch_block_t myBlock) {
if (dispatch_get_specific(kMyQueueSentinel) != NULL) {
// We're already on the serial queue
myBlock();
} else {
dispatch_sync(self.serialQueue, myBlock);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment