Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread-Local storage and iOS

My understanding is that iOS does not support __thread. There is, obviously, a way to do this using pthread_setspecific. However, is there already a template class that has implemented this functionality?

I'd ate to re-invent the wheel, especially as it won't be a simple piece of code to write.

Any links would be hugely appreciated!

Cheers

like image 362
Goz Avatar asked Jul 02 '11 15:07

Goz


1 Answers

Foundation provides -[NSThread threadDictionary]. You can use this to store thread-local Objective-C objects, which could include an NSValue wrapping the address of any dynamic storage.

Note that Cocoa is moving towards thread-blind execution of threaded code, where you submit blocks of code to be run on any available system-owned thread. This is the model used by Grand Central Dispatch and the shared NSOperationQueues. Code relying on thread-local storage will not make the best use of this model. See Apple's Concurrency Programming Guide for more info.

ETA: Starting with iOS 5 / OS X 10.7, Grand Central Dispatch gained what you could call queue-local storage via the dispatch_queue_set_specific, dispatch_queue_get_specific, and dispatch_get_specific functions. The setter allows you to supply a destructor function in addition to the value for when you set a new value for the key or when the queue is destroyed. The getter sans queue uses the current queue as context, and will repeat the lookup on the current queue's target queue if the key is not defined on the current queue (similar to how property lookup in a prototypal OO system works).

like image 116
Jeremy W. Sherman Avatar answered Sep 30 '22 01:09

Jeremy W. Sherman