What is a queue in iOS? What is the main queue?

Hi,


I am teaching students about CoreBluetooth and I came accross one of the methods to start up the central manager -> initWithDelagate:queue. I am trying to explain to them what queue is and what is the main queue and why we would set this parameter to nil, however, I cannot find a good explanation online. Could some please explain the following:


What is a queue or the concept of queue in iOS?

What is the main queue?

What about main threads? What's the difference between main queue and main thread?


Thanks,

Mohammad Khan

developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html


Or, go to developer.apple.com/videos and search for "GCD".

Accepted Answer

A computer has one central processsing unit that processes one command at a time in the order that they are requested - just like a ticket booth handles the people asking to buy tickets - one at a time, one after the other. The people line up in a "queue" - the program's request to the central processor also line up in a queue. But there can be many different kinds of requests to the central processor - for example 'the program code' and 'the stuff related to a bluetooth connection' and 'a text message just came in'. Each of these different types of commands is assigned a particular queue and they line up in that queue. The central processor handles the requests in a particular queue at one time and, upon certain directions from the operating system, can switch and handle the requests in a different queue. If the central processor finishes all yhe requests in a particular queue it can switch to another queue. There is one queue called the main queue - it gets most of the computer code and also the commands that are associated with the program's display like the UIAlerts and the touch commands - but those display requests are executed only when the other commands in the queue are completed. The central processor handles the display requests only after it has finished its other tasks, like "for(int iCount=0; iCound<1000000;iCount++)" or "wait for a bluetooth response" so sometimes the display will not be refreshed until after a long delay. There are some tasks that capture their queue and take many seconds before they release it (for example a bluetooth link). Placing such a task on the main queue could lock up the display. And, alternatively, when such a task is started on its own queue, thereby not blocking the main queue, and it wants to display something (e.g. UIAlert - "waiting for a bluetooth response") - if it issues the display command on its own queue it will not be displayed until the task releases the queue. So it issues its display tasks on the main queue which will not be hung.

Thanks PBK for the information, it cleard it up a lot! Thanks QuinceyMorris for those resources as well!

I'm sorry, but the answer you've marked as "correct" isn't correct. It has multiple technical errors, even in the first few words: "A computer has one central processsing unit". (Modern Macs generally have 2-4 Intel "cores", or hardware CPUs, each of which operates as two logical CPUs. It's been many years since a Mac with only 1 CPU was released.)


It also confuses several different kinds of queues. There are instruction queues, event queues, dispatch queues, operation queues, and others, alongside all the kinds of programmer-defined queues that are frequently used in code. All of these queues operate at different levels, and to pretend that "the central processor" deals with them all in the same way is just plain wrong.


Your question was specifically about the queues referred to in the CBPeripheralManager "init(delegate:queue:)" method. That queue is a dispatch queue, which means that the answer is in the documentation for Grand Central Dispatch, which is the system framework that deals with dispatch queues.

> Your question was specifically about the queues referred to in the CBPeripheralManager "init(delegate:queue:)" method.

Actually the question, as asked, was much more general:

>I am trying to explain to them what queue is


I hope my answer, approximate, simplified, and perhaps imprecise as it was, answered that question at the desired level. I agree with QuinceyMorris that the answer had technical inaccuracies in it - it was not aimed at technical precision but rather simplified explanation. I stand corrected; but remain convinced such answers are often more useful, inaccurate as they may be, than a link to a complex document.


>I'm sorry,

No apology needed - just please understand the context of the proffered information.

>Actually the question, as asked, was much more general:

>>I am trying to explain to them what queue is


FWIW, I understood this as "I am trying to explain to them what 'queue' is" (referring to the parameter keyword in "initWithDelegate:queue:"), not "I am trying to explain to them what a queue is".


I'm not averse to approximation or simplification, and your answer had some relevance conceptually. I am mostly concerned that it may be misleading to others in the future, since it looks "validated" by being marked correct.

Agree. Thanks.

To PBK and QuinceyMorris


Thanks for your input. I appreciate both the technical and simplified translation. I am teaching this to students at University and they dont have background in CS. They are biomedical and mechanical major studenst who are devloping devices and apparatus that use Bluetooth LE to send data over to iOS.


This is why I asked for a simplified explanaion. However, I acknowledge the concerns QuinceyMorris has brought forward. I will put the disclaimer in my slide saying that this is an over simplified explanation of queues given the nature of CPUs and the differen queues in iOS.


In any case, I appreciate the help and your concerns!


Thanks

What is a queue in iOS? What is the main queue?
 
 
Q