I have been learning about threading and DispatchQueues a lot recently and have come to a big question. I have heard many times that GCD makes no guarantees about which thread a given block of work may be executed on. Most of the time, this is a helpful level abstraction. However, I'm hitting a bug which I still don't know the cause of, but which caused me to realize what seems to me to be a potential pitfall of this aspect of GCD.
Example:
let queue1 = DispatchQueue(label: "one")
let queue2 = DispatchQueue(label: "two")
queue1.sync {
let importantValue1 = "importantValue1"
let importantValue2 = queue2.sync {
return "importantValue2"
}
print("did important work, got values", importantValue1, importantValue2)
}
My question is, am I at least guaranteed that my queues will not execute on the same thread? From what I've seen it doesn't seem like I have this guarantee. But, without it, am I not in constant jeopardy of deadlock? In the example above, what would happen if both queues execute on Thread 7? Won't the call to queue2.sync
cause the app to crash?