Channels ▼

Gaston Hillar

Dr. Dobb's Bloggers

iPad 2 Brings iOS Developers to Multicore Programming Land

March 15, 2011

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.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Dr. Dobb's TV