Cocoa and Threading

You can use multiple threads in a Cocoa program. As usual, Cocoa brings a nice set of clean APIs that provide the threading features. All of the caveats above regarding race conditions and performance apply when using Cocoa, as well as some additional gotchas.

NSThread

NSThread is the class that abstracts threads. To create a new thread, use the class method

+ (void)detachNewThreadSelector: (SEL) aSelector  
                       toTarget: (id) aTarget 
                     withObject: (id) anArgument;

aSelector is a selector that describes a method aTarget can receive. aSelector's signature is

- (void) aSelector: (id) anArgument;

anArgument is what gets passed to this method. This call works just like pthread_create() in that the thread starts executing with the first ...

Get Advanced Mac OS X Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.