Thread-Local storage and iOS

我怕爱的太早我们不能终老 提交于 2019-12-04 03:11:09

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/6557768/thread-local-storage-and-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!