The following things are what I know & understand:
Global queue is a concurrent queue which can dispatch tasks to multiple threads. The order of executing task is not guaranteed. e.g.:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), {
for (int i; i<10; i++) {
doTask()
}
})
If I want to dispatch to serial queue, I can use
dispatch_async(dispatch_queue_create("my.serial.queue", nil) {
...
}
each time only one task is dispatched to a thread & get executed. The order is FIFO.
===== What I am confused & not fully understand =======
The main thread has a NSRunLoop, looping tasks in main thread. I am wondering what is the relation ship between dispatch queue and run loop? Can I understand it like, if dispatching a task to main thread, the main thread's NSRunLoop get the dispatched task and execute it?
What about global queue which dispatching tasks to multiple threads? Does iOS/OSX system automatically create not only the threads, but also create NSRunLoop for each thread? and then the run loop in each thread get the dispatched task from global queue & execute it?
Who knows the thread? Do
dispatch_async()
anddispatch_sync()
function know to which thread to dispatch task or does the queue knows to which thread to dispatch task?Is there a way to get NSRunLoop object of the thread(to which the task is dispatched) from dispatch queue programmatically? (this question is related with question 3)