-
-
Save MSch/2633653 to your computer and use it in GitHub Desktop.
dispatch_reentrant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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