线程本地存储和 iOS
我的理解是iOS不支持__thread
。显然,有一种方法可以使用 pthread_setspecific 来做到这一点。然而,是否已经有一个模板类实现了这个功能呢?
我愿意重新发明轮子,特别是因为它不是一段简单的代码。
任何链接将不胜感激!
干杯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Foundation 提供了
-[NSThread threadDictionary]
。您可以使用它来存储线程本地 Objective-C 对象,其中可能包括包装任何动态存储地址的NSValue
。请注意,Cocoa 正在朝着线程代码的线程盲执行方向发展,您可以提交要在任何可用的系统拥有的线程上运行的代码块。这是 Grand Central Dispatch 和共享 NSOperationQueue 使用的模型。依赖线程本地存储的代码将无法充分利用此模型。请参阅 Apple 的并发编程指南,了解更多信息。
预计到达时间:从 iOS 5 / OS X 10.7 开始,Grand Central Dispatch 通过
dispatch_queue_set_specific
、dispatch_queue_get_specific
获得了所谓的队列本地存储。和dispatch_get_specific 函数。当您为键设置新值或销毁队列时,除了值之外,setter 还允许您提供析构函数。 getter sans 队列使用当前队列作为上下文,如果当前队列上未定义键,则将在当前队列的目标队列上重复查找(类似于原型 OO 系统中属性查找的工作方式)。Foundation provides
-[NSThread threadDictionary]
. You can use this to store thread-local Objective-C objects, which could include anNSValue
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
NSOperationQueue
s. 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
, anddispatch_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).