iPad 2 Brings iOS Developers to Multicore Programming Land
The recently launched iPad 2 includes a 1 GHz dual-core Apple A5 SoC (short for System-on-a-Chip). If you want your iOS apps to take full advantage of this powerful dual-core chip, you have to use multicore programming techniques in these apps. If you code in Objective-C, the newest iOS versions allow you to use the new language features introduced with Apple's Grand Central Dispatch.
Apple introduced Grand Central Dispatch, also known as GCD, in Mac OS X Snow Leopard (version 10.6). Grand Central Dispatch introduced a group of FIFO (acronym for First-In First-Out) queues to which an application can submit tasks in the form of block objects. The main goal for GCD was to make it simple for diverse kinds of applications to take better advantage of multicore CPUs by offering a task-based programming approach with under the hoods thread pool management. GCD introduced new language features and run-time libraries. iOS 4 made GCD available for iOS developers, and therefore, the operating system for iPad 2, iOS 4.3, supports GCD. If you want to transform the multicore power of the dual-core Apple A5 SoC into app performance, GCD can help you.
GCD removes the need for an application to create and manage threads because GCD creates the appropriate number of threads to execute the queued tasks. GCD includes complex load-balancing mechanisms that consider the underlying hardware capabilities and do their best to keep the whole system more responsive.
However, the FIFO queue might confuse developers and architects. When you queue tasks for dispatch, you can choose between three kinds of queues:
- Main Queue: The tasks execute serially on the application's main thread.
- Serial Queue: The tasks execute one at a time on a first-come first-served basis. The tasks run with a sequential execution one after the other.
- Concurrent Queue: The tasks start executing on a first-come first-served basis but they can run concurrently. The concurrent execution will vary according to the number of available cores. The Concurrent Queue allows you to launch many tasks in parallel to take advantage of multiple cores. The FIFO order used to start executing the tasks doesn't have a huge impact on overall scalability and performance. GCD uses creates many Concurrent Queues for an application, and therefore, if you need to launch a large number of tasks with a concurrent execution, you can use Concurrent Queues with different priority levels. GCD will create and manage the appropriate number of threads to support the execution of these tasks according to the available hardware resources and the current workload.
You can combine the different kinds of queues to organize the execution of multiple tasks and to create critical sections. You can run blocks or functions as synchronous or asynchronous tasks. This way, you can avoid creating and managing POSIX threads to create concurrent code that takes advantage of the new dual-core SoC. However, GCD is a C level API. If you are creating high-level code, you can use the operation queues. Operation queues are Objective-C objects that act like dispatch queues.
You can read the detailed documentation about best-practices with operation queues in the "Concurrency and Application Design" section of the "Concurrency Programming Guide." However, this guide hasn't been updated with the importance of task-based programming for iPad 2. iPad 2 has a dual-core SoC and task-based programming is very important for this new device.
You can use task-based programming techniques when you create apps for iPad 2 and you will benefit from the two cores. iPhone 5 will probably have a dual-core SoC, too. Thus, if you target modern and future iOS devices, you should start mastering multicore programming techniques for this platform soon. Don't create and manage POSIX threads from scratch before reading about GDC capabilities. In fact, you will find task-based programming with GDC useful for achieving performance enhancements and scalability in most apps.
James Reinders also wrote an article on Grand Central Dispatch two years ago, and you can read it here.