Synchronous dispatch without deadlocking

March 4, 2013 in iOS Snippets

Dispatches synchronously without deadlocking if the target thread is the current thread.


static inline void dispatch_sync2(dispatch_queue_t queue, dispatch_block_t block) {
 if (dispatch_get_current_queue() == queue) {
 block();
 } else {
 dispatch_sync(queue, block);
 }
}