O'Reilly logo

Advanced Mac OS X Programming: The Big Nerd Ranch Guide by Mark Dalrymple

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required