background preloader

Threading & GCD

Facebook Twitter

Grand Central Dispatch. The name "Grand Central Dispatch" is a reference to Grand Central Terminal.

Grand Central Dispatch

[citation needed] The source code for the library that provides the implementation of GCD's services, libdispatch, was released by Apple under the Apache License on September 10, 2009[3] It has been ported[4] to the FreeBSD operating system, starting with FreeBSD 8.1.[5] MidnightBSD 0.3-CURRENT includes "libdispatch" without blocks support.[6] Linux and Solaris support are provided within the upstream trunk.[7][8] In order to develop support for Windows, currently two forks exist at opensource.mlba-team.de and Github.[9][10] Apple has its own port of libdispatch.dll for Windows shipped with Safari and iTunes, but no SDK is provided.

Design[edit] Grand Central Dispatch still uses threads at the low level but abstracts them away from the programmer, who will not need to be concerned with as many details. Features[edit] The dispatch framework declares several data types and functions to create and manipulate them: GCD Design Patterns. Practical Design Patterns with Blocks and Grand Central Dispatch.

When Mac OS X 10.6 was introduced, the Mac got a very powerful duo of developer tools that made development on a lot easier. With iOS 4.0 developers finally got access to these tools as well. These tools are known as Blocks & Grand Central Dispatch. This article is not an introduction to these technologies as i've already covered them before here What this article is, is a introduction to some design patterns I use and have seen other developers use in 1 handy article. Many of the design patterns I am displaying here are actually in use in my Zangetsu Framework which is open source under the MIT License on Github.Iterative Callbacks /** Ruby inspired iterator for NSArray in Objective-C */ -(NSArray *)cw_each:(void (^)(id obj))block { for(id object in self){ block(object); } return self; } Blocks are really good for doing callbacks and one of the things you can do already is enumeration.

Here is a good example of putting that callback capability to good use. This way in code I can do. Threading Programming. Threads are one of several technologies that make it possible to execute multiple code paths concurrently inside a single application.

Threading Programming

Although newer technologies such as operation objects and Grand Central Dispatch (GCD) provide a more modern and efficient infrastructure for implementing concurrency, OS X and iOS also provide interfaces for creating and managing threads. This document provides an introduction to the thread packages available in OS X and shows you how to use them. This document also describes the relevant technologies provided to support threading and the synchronization of multithreaded code inside your application. Organization of This Document This document has the following chapters and appendixes: “About Threaded Programming” introduces the concept of threads and their role in application design. See Also For information about the alternatives to threads, see Concurrency Programming Guide. The Run Loop. Run loops are part of the fundamental infrastructure associated with threads.

The Run Loop

A run loop is an event processing loop that you use to schedule work and coordinate the receipt of incoming events. The purpose of a run loop is to keep your thread busy when there is work to do and put your thread to sleep when there is none. NSCondition. Overview The NSCondition class implements a condition variable whose semantics follow those used for POSIX-style conditions.

NSCondition

A condition object acts as both a lock and a checkpoint in a given thread. The lock protects your code while it tests the condition and performs the task triggered by the condition. The checkpoint behavior requires that the condition be true before the thread proceeds with its task. While the condition is not true, the thread blocks. The semantics for using an NSCondition object are as follows: Lock the condition object.Test a boolean predicate. The pseudocode for performing the preceding steps would therefore look something like the following: Whenever you use a condition object, the first step is to lock the condition. When a thread waits on a condition, the condition object unlocks its lock and blocks the thread.

A boolean predicate is an important part of the semantics of using conditions because of the way signaling works. Instance Methods broadcast Discussion.