Skip to content

Instantly share code, notes, and snippets.

@MSch
Forked from steipete/gist:1205760
Created May 8, 2012 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MSch/2633653 to your computer and use it in GitHub Desktop.
Save MSch/2633653 to your computer and use it in GitHub Desktop.
dispatch_reentrant
// checks if already in current queue, prevents deadlock
void dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_block_t block) {
if (dispatch_get_current_queue() == queue) {
block();
}else {
dispatch_sync(queue, block);
}
}
// problem:
void not_really_reentrant() {
dispatch_queue_t queueA = ...;
dispatch_queue_t queueB = ...;
dispatch_sync_reentrant(queueA, ^{
dispatch_sync_reentrant(queueB, ^{
dispatch_sync_reentrant(queueA, ^{
// never reached
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment