Can thread be used analogously with queue, or do they mean separate things?

55 Views Asked by At

I was reading through this great tutorial when I came accross the following line (as background: we were learning how to use dispatch_apply to replace a for loop and concurrently download photos):

Be aware that although you have code that will add the photos in a thread safe manner, the ordering of the images could be different depending on which thread finishes first.

This line really threw me off for some reason. I thought dispatch_apply will run a task on one concurrent thread, GlobalUserInitiatedQueue, not multiple different threads. He calls the method by saying:

 dispatch_apply(addresses.count, GlobalUserInitiatedQueue) {

So is GlobalUserInitiatedQueue one thread, multiple threads, and what's the difference between a thread and a queue? From what it seems, they're used analogously. Does that mean a concurrent queue has multiple threads running at the same time?

Thanks -

1

There are 1 best solutions below

4
On BEST ANSWER

From the guide:

"Concurrent queues (also known as a type of global dispatch queue) execute one or more tasks concurrently, but tasks are still started in the order in which they were added to the queue. The currently executing tasks run on distinct threads that are managed by the dispatch queue. The exact number of tasks executing at any given point is variable and depends on system conditions."

It can't be clearer than that. Multiple threads, start in the order of the tasks on the queue but finishing in no specific order.

I love tutorials and material that is aim to explains stuff in great details. However, when in doubts, I always go back to the Apple official documentations.