61

I ran into a scenario where I had a delegate callback which could occur on either the main thread or another thread, and I wouldn't know which until runtime (using StoreKit.framework).

I also had UI code that I needed to update in that callback which needed to happen before the function executed, so my initial thought was to have a function like this:

-(void) someDelegateCallback:(id) sender
{
    dispatch_sync(dispatch_get_main_queue(), ^{
        // ui update code here
    });

    // code here that depends upon the UI getting updated
}

That works great, when it is executed on the background thread. However, when executed on the main thread, the program comes to a deadlock.

That alone seems interesting to me, if I read the docs for dispatch_sync right, then I would expect it to just execute the block outright, not worrying about scheduling it into the runloop, as said here:

As an optimization, this function invokes the block on the current thread when possible.

But, that's not too big of a deal, it simply means a bit more typing, which lead me to this approach:

-(void) someDelegateCallBack:(id) sender
{
    dispatch_block_t onMain = ^{
        // update UI code here
    };

    if (dispatch_get_current_queue() == dispatch_get_main_queue())
       onMain();
    else
       dispatch_sync(dispatch_get_main_queue(), onMain);
}

However, this seems a bit backwards. Was this a bug in the making of GCD, or is there something that I am missing in the docs?

4
  • 5
    dispatch_get_current_queue() is deprecated by now. The way to go for detecting the main queue is NSThread.isMainThread() (Swift) or [NSThread isMainThread] (Objective-C)
    – udondan
    Feb 22, 2015 at 4:39
  • NSThread.isMainThread() is not reliable because in rare cases the main queue blocks, and GCD reuses the main thread to execute other queues. See 1, 2.
    – Jano
    Jun 10, 2016 at 9:04
  • @jtbandes please be careful when marking questions as duplicates. This question is clearly older and has much more activity than the one you linked, and perhaps they should be closed in the reverse direction. Jun 29, 2016 at 22:45
  • 1
    @RichardJ.RossIII: I did consider that; IMO the one I duped it to was an easier-to-understand question, with a more thorough answer. This topic is discussed at meta.stackoverflow.com/questions/315472/…
    – jtbandes
    Jun 29, 2016 at 23:59

6 Answers 6

77

dispatch_sync does two things:

  1. queue a block
  2. blocks the current thread until the block has finished running

Given that the main thread is a serial queue (which means it uses only one thread), if you run the following statement on the main queue:

dispatch_sync(dispatch_get_main_queue(), ^(){/*...*/});

the following events will happen:

  1. dispatch_sync queues the block in the main queue.
  2. dispatch_sync blocks the thread of the main queue until the block finishes executing.
  3. dispatch_sync waits forever because the thread where the block is supposed to run is blocked.

The key to understanding this issue is that dispatch_sync does not execute blocks, it only queues them. Execution will happen on a future iteration of the run loop.

The following approach:

if (queueA == dispatch_get_current_queue()){
    block();
} else {
    dispatch_sync(queueA, block);
}

is perfectly fine, but be aware that it won't protect you from complex scenarios involving a hierarchy of queues. In such case, the current queue may be different than a previously blocked queue where you are trying to send your block. Example:

dispatch_sync(queueA, ^{
    dispatch_sync(queueB, ^{
        // dispatch_get_current_queue() is B, but A is blocked, 
        // so a dispatch_sync(A,b) will deadlock.
        dispatch_sync(queueA, ^{
            // some task
        });
    });
});

For complex cases, read/write key-value data in the dispatch queue:

dispatch_queue_t workerQ = dispatch_queue_create("com.meh.sometask", NULL);
dispatch_queue_t funnelQ = dispatch_queue_create("com.meh.funnel", NULL);
dispatch_set_target_queue(workerQ,funnelQ);

static int kKey;
 
// saves string "funnel" in funnelQ
CFStringRef tag = CFSTR("funnel");
dispatch_queue_set_specific(funnelQ, 
                            &kKey,
                            (void*)tag,
                            (dispatch_function_t)CFRelease);

dispatch_sync(workerQ, ^{
    // is funnelQ in the hierarchy of workerQ?
    CFStringRef tag = dispatch_get_specific(&kKey);
    if (tag){
        dispatch_sync(funnelQ, ^{
            // some task
        });
    } else {
        // some task
    }
});

Explanation:

  • I create a workerQ queue that points to a funnelQ queue. In real code this is useful if you have several “worker” queues and you want to resume/suspend all at once (which is achieved by resuming/updating their target funnelQ queue).
  • I may funnel my worker queues at any point in time, so to know if they are funneled or not, I tag funnelQ with the word "funnel".
  • Down the road I dispatch_sync something to workerQ, and for whatever reason I want to dispatch_sync to funnelQ, but avoiding a dispatch_sync to the current queue, so I check for the tag and act accordingly. Because the get walks up the hierarchy, the value won't be found in workerQ but it will be found in funnelQ. This is a way of finding out if any queue in the hierarchy is the one where we stored the value. And therefore, to prevent a dispatch_sync to the current queue.

If you are wondering about the functions that read/write context data, there are three:

  • dispatch_queue_set_specific: Write to a queue.
  • dispatch_queue_get_specific: Read from a queue.
  • dispatch_get_specific: Convenience function to read from the current queue.

The key is compared by pointer, and never dereferenced. The last parameter in the setter is a destructor to release the key.

If you are wondering about “pointing one queue to another”, it means exactly that. For example, I can point a queue A to the main queue, and it will cause all blocks in the queue A to run in the main queue (usually this is done for UI updates).

6
  • 2
    Obviously that is correct. dispatch_sync is almost never the way to go, I've only needed it a few times to update & get results from the UI section of my application, past that, you need to choose something else. Your crazy technique for checking the queue hierarchy is probably just going to lead to pain down the road. Mar 31, 2013 at 3:57
  • 1
    It's convoluted and I rather have a built-in amIChildOfQueue:, but using queue-specific context is the solution recommended by Apple for complex cases. See post #6 in the thread dispatch_get_current_queue() deprecated.
    – Jano
    Mar 31, 2013 at 16:14
  • Can you have a look at this question? stackoverflow.com/questions/19833744/…
    – hfossli
    Nov 7, 2013 at 10:41
  • @Jano what do you mean by "blocks the current queue until the block has finished running"?? People say that dispatch_sync only blocks the current thread on which it is called
    – onmyway133
    May 29, 2014 at 17:19
  • 1
    @entropy The statement you quote is only true if the queue has only one thread, eg: the main queue. I edited the answer to clarify.
    – Jano
    May 29, 2014 at 20:44
53

I found this in the documentation (last chapter):

Do not call the dispatch_sync function from a task that is executing on the same queue that you pass to your function call. Doing so will deadlock the queue. If you need to dispatch to the current queue, do so asynchronously using the dispatch_async function.

Also, I followed the link that you provided and in the description of dispatch_sync I read this:

Calling this function and targeting the current queue results in deadlock.

So I don't think it's a problem with GCD, I think the only sensible approach is the one you invented after discovering the problem.

7
  • 12
    I must say I don't agree that there is something wrong with how dispatch_sync behaves. If you think about it, both dispatch_sync and async queue the tasks, but the first one also does not return until the task is executed. In the example you provided, the task is queued but never executed, and this is the direct reason for the deadlock. So remember that the main functionality of this function is to actually queue the task, not to invoke it. The invocation is a different story, but from what you write it looks like you expect this function to actually invoke you task.
    – lawicko
    Jun 12, 2012 at 14:19
  • 9
    I disagree. I have no real interest on how dispatch_sync works under the covers, I care that, from a top-down look, what it does is execute this code on the given thread, and return when it's done. If I am on the target thread, it makes NO sense for me to have to check if I'm on the target thread, as the function should do it for me. It really surprises me, though because most of apple's APIs are smarter than this, I guess the devs just got lazy on the job? :) Jun 12, 2012 at 14:22
  • 9
    @RichardJ.RossIII, you seem to be ignoring the fact that the API you're using is a serial queue, and you're trying to block the current item on that queue while you wait for the item behind it to execute. The fact that the API doesn't do what you want it to do doesn't mean it's poorly implemented. It does exactly what it's documented to do. Jun 12, 2012 at 16:24
  • 11
    @Richard: I believe that the error in your thinking is here: «I care that, from a top-down look, what it does is execute this code on the given thread, and return when it's ». dispatch_sync() doesn't work with threads, it works with queues. The fact that the main queue is guaranteed to be run on the main thread is coincidence from dispatch_sync()'s point of view. For it to immediately execute the block you're trying to enqueue would break its meaning -- executing the next task before the current one has completed means you would no longer have queue behavior.
    – jscs
    Jun 13, 2012 at 0:28
  • 2
    The problem is, 99.9% of the time, nobody really wants true serial queue semantics. They don't care about order; they just want no concurrency. There are situations where the dispatch_sync semantics make sense, but I would argue that they cause problems far more often than they help. That said, if you just want to run some code on the main thread, performSelectorOnMainThread: has the semantics you're looking for. Or just write #define dispatch_sync_safe(queue, block) {if (queue == dispatch_get_current_queue()) { block(); } else { dispatch_sync(queue, block);}} and call that instead.
    – dgatwood
    Sep 21, 2016 at 23:56
16

I know where your confusion comes from:

As an optimization, this function invokes the block on the current thread when possible.

Careful, it says current thread.

Thread != Queue

A queue doesn't own a thread and a thread is not bound to a queue. There are threads and there are queues. Whenever a queue wants to run a block, it needs a thread but that won't always be the same thread. It just needs any thread for it (this may be a different one each time) and when it's done running blocks (for the moment), the same thread can now be used by a different queue.

The optimization this sentence talks about is about threads, not about queues. E.g. consider you have two serial queues, QueueA and QueueB and now you do the following:

dispatch_async(QueueA, ^{
    someFunctionA(...);
    dispatch_sync(QueueB, ^{
        someFunctionB(...);
    });
});

When QueueA runs the block, it will temporarily own a thread, any thread. someFunctionA(...) will execute on that thread. Now while doing the synchronous dispatch, QueueA cannot do anything else, it has to wait for the dispatch to finish. QueueB on the other hand, will also need a thread to run its block and execute someFunctionB(...). So either QueueA temporarily suspends its thread and QueueB uses some other thread to run the block or QueueA hands its thread over to QueueB (after all it won't need it anyway until the synchronous dispatch has finished) and QueueB directly uses the current thread of QueueA.

Needless to say that the last option is much faster as no thread switch is required. And this is the optimization the sentence talks about. So a dispatch_sync() to a different queue may not always cause a thread switch (different queue, maybe same thread).

But a dispatch_sync() still cannot happen to the same queue (same thread, yes, same queue, no). That's because a queue will execute block after block and when it currently executes a block, it won't execute another one until the currently executed is done. So it executes BlockA and BlockA does a dispatch_sync() of BlockB on the same queue. The queue won't run BlockB as long as it still runs BlockA, but running BlockA won't continue until BlockB has ran. See the problem? It's a classical deadlock.

6

The documentation clearly states that passing the current queue will cause a deadlock.

Now they don’t say why they designed things that way (except that it would actually take extra code to make it work), but I suspect the reason for doing things this way is because in this special case, blocks would be “jumping” the queue, i.e. in normal cases your block ends up running after all the other blocks on the queue have run but in this case it would run before.

This problem arises when you are trying to use GCD as a mutual exclusion mechanism, and this particular case is equivalent to using a recursive mutex. I don’t want to get into the argument about whether it’s better to use GCD or a traditional mutual exclusion API such as pthreads mutexes, or even whether it’s a good idea to use recursive mutexes; I’ll let others argue about that, but there is certainly a demand for this, particularly when it’s the main queue that you’re dealing with.

Personally, I think that dispatch_sync would be more useful if it supported this or if there was another function that provided the alternate behaviour. I would urge others that think so to file a bug report with Apple (as I have done, ID: 12668073).

You can write your own function to do the same, but it’s a bit of a hack:

// Like dispatch_sync but works on current queue
static inline void dispatch_synchronized (dispatch_queue_t queue,
                                          dispatch_block_t block)
{
  dispatch_queue_set_specific (queue, queue, (void *)1, NULL);
  if (dispatch_get_specific (queue))
    block ();
  else
    dispatch_sync (queue, block);
}

N.B. Previously, I had an example that used dispatch_get_current_queue() but that has now been deprecated.

6
  • I have done similar, except with a macro, so that other code that I had written that used dispatch_sync wasn't broken. +1 to you! Nov 9, 2012 at 2:13
  • 1
    A macro would work equally well, but generally speaking, I would advise you to only use a macro when you can’t use a static inline function as they are preferable for many reasons and macros offer no advantages. Nov 9, 2012 at 5:34
  • 1
    dispatch_get_current_queue is deprecated since iOS 6.x
    – openfrog
    Jun 28, 2013 at 13:24
  • That doesn't prevent deadlocks because you could have a queue whose target queue is queue. Then you'd go into the else branch and then deadlock. It's also documented by Apple: It is equally unsafe for code to assume that synchronous execution onto a queue is safe from deadlock if that queue is not the one returned by dispatch_get_current_queue(). from man 3 dispatch_get_current_queue under CAVEATS. Nov 9, 2013 at 23:30
  • 2
    For the main queue you could however use if ([NSThread isMainThread]) { block() } else { dispatch_sync(dispatch_get_main_queue(), block); } which is safe because all queues that have the main queue as its target are also executing on the main thread (because main queue is a serial queue). Nov 9, 2013 at 23:35
4

Both dispatch_async and dispatch_sync perform push their action onto the desired queue. The action does not happen immediately; it happens on some future iteration of the run loop of the queue. The difference between dispatch_async and dispatch_sync is that dispatch_sync blocks the current queue until the action finishes.

Think about what happens when you execute something asynchronously on the current queue. Again, it does not happen immediately; it puts it in a FIFO queue, and it has to wait until after the current iteration of the run loop is done (and possibly also wait for other actions that were in the queue before you put this new action on).

Now you might ask, when performing an action on the current queue asynchronously, why not always just call the function directly, instead of wait until some future time. The answer is that there is a big difference between the two. A lot of times, you need to perform an action, but it needs to be performed after whatever side effects are performed by functions up the stack in the current iteration of the run loop; or you need to perform your action after some animation action that is already scheduled on the run loop, etc. That's why a lot of times you will see the code [obj performSelector:selector withObject:foo afterDelay:0] (yes, it's different from [obj performSelector:selector withObject:foo]).

As we said before, dispatch_sync is the same as dispatch_async, except that it blocks until the action is completed. So it's obvious why it would deadlock -- the block cannot execute until at least after the current iteration of the run loop is finished; but we are waiting for it to finish before continuing.

In theory it would be possible to make a special case for dispatch_sync for when it is the current thread, to execute it immediately. (Such a special case exists for performSelector:onThread:withObject:waitUntilDone:, when the thread is the current thread and waitUntilDone: is YES, it executes it immediately.) However, I guess Apple decided that it was better to have consistent behavior here regardless of queue.

2
  • But this makes no sense. There should be at least a log message outputted to the console in case of a mistake, as there is with other APIs (recursive NSLock ing, for example). Jun 11, 2012 at 20:03
  • @newacct "dispatch_sync blocks the current thread"? Block the current thread or the current queue ?
    – onmyway133
    May 29, 2014 at 16:44
2

Found from the following documentation. https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html#//apple_ref/c/func/dispatch_sync

Unlike dispatch_async, "dispatch_sync" function does not return until the block has finished. Calling this function and targeting the current queue results in deadlock.

Unlike with dispatch_async, no retain is performed on the target queue. Because calls to this function are synchronous, it "borrows" the reference of the caller. Moreover, no Block_copy is performed on the block.

As an optimization, this function invokes the block on the current thread when possible.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.