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

For the More Curious: Thread Local Storage

Sometimes it is very handy to have storage that is private to a thread. In the basics.m program above, you could stash the contents of the ThreadInfo stack into thread local storage instead of on the function call stack. Thread local storage behaves like global variables but are private to the thread. errno is stored in thread local storage so that every thread has its own errno.

To use thread local storage, initialize a pthread_key_t using pthread_key_create():

int pthread_key_create (pthread_key_t *key, void (*destructor)(void *));

This creates an abstract key. Usually you name a variable with a descriptive name or associate this key with a descriptive string in a dictionary, as shown in Figure 20.5 ...

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