I know how hassle is to create a multithreaded application especially on mobile devices. Creating, managing, and releasing thread objects is not always as easy as other objects, so the other alternative which can be considered as best option is Grand Central Dispatch also often referred as GCD. GCD is enormously powerful and written in C. GCD manages pools of threads known as "dispatch queues", these dispatch queues are generally of three types,
Main queue: all tasks in this queue are performed on main thread, programmer to call all UI related methods in this queue, dispatch_getmain_queue is used to retrieve the handle to the main queue.
Concurrent queue: synchronous and asynchronous tasks can be executed in this queue, multiple concurrent queues can execute multiple tasks, dispatch_get_global_queue is used to get the concurrent queues.
Serial queues: it is irrelevant if the tasks are synchronous or asynchronous, always performs FIFO (First task In is the First task Out). This is perfect for series of tasks that need to be executed in strict order. dispatch_queue_createis used to get the serial queues.
The main advantages of GCD:
Main queue: all tasks in this queue are performed on main thread, programmer to call all UI related methods in this queue, dispatch_getmain_queue is used to retrieve the handle to the main queue.
Concurrent queue: synchronous and asynchronous tasks can be executed in this queue, multiple concurrent queues can execute multiple tasks, dispatch_get_global_queue is used to get the concurrent queues.
Serial queues: it is irrelevant if the tasks are synchronous or asynchronous, always performs FIFO (First task In is the First task Out). This is perfect for series of tasks that need to be executed in strict order. dispatch_queue_createis used to get the serial queues.
The main advantages of GCD:
- Tasks can be dispatched to multiple cores and best part of it is, as a programmer you need not worry about which core is executing which task.
- GCD offers running several tasks synchronously, asynchronously, after a certain delay, etc.Apple has already incorporated GCD into various frameworks, so no separate importing required.
- As the new iPhone 4s and iPad2 works on multi core processors, it is ideal to use GCD to take advantage of core processors.