3 ms·
The waiting queue mechanism in the article is related to the specific requirements of that auth network request rather than Swift in general. The queues provid
by jchb 8y ago
The waiting queue mechanism in the article is related to the specific requirements of that auth network request rather than Swift in general.
The queues provided by Apples GCD are an abstraction above threads and mutexes.
The equivalent to your critical section example is:
let dispatchQueue = DispatchQueue(label: "com.mycompany.myComponent") // Initialize queue, just as you would with the mutex, label will show in debugger
dispatchQueue.sync {
// Critical section
}
There are several wins here. One is that gcd controls thread creation as needed. But the bigger win is when you have a longer running critical section and mustn't block the calling UI (main) queue.
Then you can simply do an async dispatch on the queue and callback to the UI queue on completion. GCD will also introduce the necessary memory barriers for passing data between the queues.
- coldcode 8y agoI find the lack of language level locking a benefit in Swift, using GCD is really easy now that the syntax is much more polished. I have yet to run into a race condition. When I worked (years ago) on a 3D game engine in C++ we actually removed most of the multi-threading because it was too hard to reason what it was doing and avoid threading errors and race conditions.