Search code examples
iosmultithreadingthreadpoolgrand-central-dispatchqos

GCD: does thread only has two types? main thread and background thread


Context:

From my understanding, the Main DispatchQueue only dispatch tasks on Main Thread which is for UI mostly.

However the Main Thread can also be used by non-main DispatchQueues.

Apple has QOS to priorities the tasks:

User Interactive: Work that happens on the main thread, such as animations or drawing operations.

User Initiated: Work that the user kicks off and should yield immediate results. This work must be completed for the user to continue.

Utility: Work that may take a bit and doesn’t need to finish right away. Analogous to progress bars and importing data.

Background: This work isn’t visible to the user. Backups, syncs, indexing, etc.

My Question Is:

0, as title described, does thread only contain 2 types, which are Main Thread and Background Thread?

1, does Background Thread mean that all the tasks executed won't block the UI ?

2, Since it mentions that the User Interactive is the priority that the task will be executed on the main thread to avoid UI lags, does it mean all other types: User Initiated, Utility, Background will make sure to have Background Thread that does not block UI?

3, From the link, How to create dispatch queue in Swift 3. It mentions a couple of different ways to create a Dispatch Queue, some are concurrent, some are serial. It also mentions that by assigning QOS with default or background, it guarantees that the Queue accesses to background threads. But nothing like this mentioned in the Serial and Concurrent. I wonder are those correct?


Solution

  • You ask:

    does thread only contain 2 types, which are Main Thread and Background Thread?

    I’m not sure if I’d describe them as different “types”, but from the application developer’s perspective, yes, there is the “main” thread, dedicated to the UI, the main run loop, etc., and there are all other threads, which are, in contrast and by definition, “background” threads.

    We always want to be careful as to what we run the main queue (i.e. don’t run anything on it that could block that thread and effectively block the UI). In practice, we want to avoid doing anything on the main queue that could block it for more than a few milliseconds, at most.

    does Background Thread mean that all the tasks executed won't block the UI?

    Effectively, yes. But we’re dealing with limited resources on our devices and therefore the developers must be judicious. For example, for our background tasks, we want to use a QoS that is commensurate for what that queue is doing. Or if parallelizing some task, one should be careful about constraining the degree of concurrency. But if one is judicious about the use of system resources, and keep blocking tasks off the main queue, then that can ensure a responsive UI.

    Bottom line, yes, if you have some code that would block the thread on which its run (e.g. it’s computationally expensive, it has blocking calls like semaphores or dispatch group wait, etc.), you would generally run that on a background thread to avoid blocking the main queue.

    Since it mentions that the User Interactive is the priority that the task will be executed on the main thread to avoid UI lags, does it mean all other types: User Initiated, Utility, Background will make sure to have Background Thread that does not block UI?

    These are just “quality of service” levels, which are just relative priorities for queues. It’s not a question of “block the UI” or “not”, but rather just a matter of how GCD prioritizes and allocates resources.

    From the link, How to create dispatch queue in Swift 3. It mentions a couple of different ways to create a Dispatch Queue, some are concurrent, some are serial...

    Yes

    ... It also mentions that by assigning QOS with default or background, it guarantees that the Queue accesses to background threads...

    This doesn’t quite make sense. Perhaps you can share the specific excerpt from the docs and we can help you interpret what they were trying to say.

    ... But nothing like this mentioned in the Serial and Concurrent. I wonder are those correct?

    The QoS is just a question of priority and resources for a given queue. Serial vs concurrent is just a question of whether a queue is limited to a single thread at a time, or whether it can avail itself of multiple threads when needed and available.