First of all, I really don’t think you wanted to make this queue parallel. dispatch_sync() Joining a parallel queue doesn’t actually do a lot of things (parallel queues do not guarantee the order between the blocks running on them). So the rest of this answer suggests that you should have another queue. In addition, I am going to answer this in general terms, and not your specific questions; hope good :)
There are two main problems when using dispatch_get_current_queue() . One very wide one, which can be summed up as “recursive locking is a bad idea” and one specific to sending, which can be summed up as “you can and will often have more than one current queue”.
Problem # 1: Recursive locking is a bad idea
The usual goal of a private sequential queue is to protect the invariant of your code (an “invariant” that is “something that must be true”). For example, if you use a queue to protect access to a property so that it is thread safe, then the invariant “this property does not have an invalid value” (for example: if the property is a structure, then half the structure may have a new value, and half may have the old value if it was specified from two threads at the same time. A sequential queue forces one thread or the other to finish tuning the entire structure before the other can start).
We can conclude that in order for this to make sense, the invariant must be executed at the beginning of the execution of the block in a sequential queue (otherwise it was clearly not protected). As soon as the block starts to execute, it can break the invariant (for example, set the property) without fear of spoiling any other threads if the invariant is held back by the time it returns (in this example, the property must be fully set).
To summarize, just to make sure that you are still following: at the beginning and end of each block in a sequential queue, the invariant protecting the queue must be preserved. In the middle of each block, it can be broken.
If inside a block you call something that is trying to use a thing protected by a queue, then you have changed this simple rule to a much more complex one: instead of "at the beginning and at the end of each" it "block at the beginning, at the end, and at any point where this a block causes something outside of itself In other words, instead of thinking about your thread safety at the block level, you now need to examine each individual line of each block.
What does this have to do with dispatch_get_current_queue() ? The only reason to use dispatch_get_current_queue() here is to check "are we already in this queue?", And if you are already in the current queue, then you are already in a terrible situation above! So don’t do it. Use private queues to protect things and do not call them from other code. You should already know the answer to the question: "Am I in this line?" and it should be no.
This is the biggest reason dispatch_get_current_queue() deprecated: so people don’t try to simulate a recursive lock (what I described above) with it.
Problem number 2: you can have more than one current queue!
Consider this code:
dispatch_async(queueA, ^{ dispatch_sync(queueB, ^{
Obviously, queueB is current, but we are still in the queue! dispatch_sync makes queueA wait for queueB to complete, so they are both effectively "current".
This means that this code will be blocked:
dispatch_async(queueA, ^{ dispatch_sync(queueB, ^{ dispatch_sync(queueA, ^{}); }); });
You can also have several current queues using target queues:
dispatch_set_target_queue(queueB, queueA); dispatch_sync(queueB, ^{ dispatch_sync(queueA, ^{ }); });
What is really needed here is something like the hypothetical " dispatch_queue_is_synchronous_with_queue(queueA, queueB) ", but since it would only be useful for implementing recursive locking, and I have already described how this bad idea ... is unlikely to be added.
Please note: if you use only dispatch_async() , then you are immune to deadlocks. Unfortunately, you are not at all protected from race conditions.