iPhone多线程AddressBook操作 [英] iPhone multi-threaded AddressBook manipulation
问题描述
我一直在使用iPhone的AddressBook api。但是为了提高应用程序性能做了一些重构,我决定重用AddressBookCreate返回的ABAddressBookRef,因为我注意到有很大的性能改进。但是,我现在正在随机获得EXEC_BAD_ACCESS错误,我认为原因在于iPhone参考实现中的这个警告: http://developer.apple.com/iphone/library/documentation/ContactData/ Conceptual / AddressBookProgrammingGuideforiPhone / 300-BasicObjects / BasicObjects.html#// apple_ref / doc / uid / TP40007744-CH3-SW1
I have been using the AddressBook api of the iPhone for some time now. But doing some refactoring to improve application performance I have decided to "reuse" the ABAddressBookRef returned by AddressBookCreate because I noticed there are large performance improvements doing that. However, I am getting EXEC_BAD_ACCESS errors now randomly, and I think the reason is in this "caveat" in the iPhone reference implementation: http://developer.apple.com/iphone/library/documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/300-BasicObjects/BasicObjects.html#//apple_ref/doc/uid/TP40007744-CH3-SW1
重要:ABAddressBookRef的实例不能由多个线程使用。每个线程必须通过调用ABAddressBookCreate来创建自己的实例。
Important: Instances of ABAddressBookRef cannot be used by multiple threads. Each thread must make its own instance by calling ABAddressBookCreate.
现在,我认为这只是意味着它不是线程安全的,所以我必须同步访问API,但是也许我错了,还有一些其他原因会导致多个线程弄乱数据结构?
Now, I thought that simply meant it was not thread-safe so I had to synchronise access to the API, but maybe I am wrong, and there is some other reasons multiple threads mess up the data structure?
有人可以确认它是否确实是一个线程安全问题(因此@synchronize应该有效)或其他一些问题?
Can someone confirm if it is indeed a thread-safe issue (so @synchronize should work) or some other issue?
干杯
推荐答案
这不是线程安全问题...你无法解决它锁。评论非常清楚:
This is not a thread safety issue... there is no way for you to solve it with locks. The comment makes it pretty clear:
重要:
倍数不能使用ABAddressBookRef的实例线程。每个线程必须
通过调用
ABAddressBookCreate来创建自己的实例。
Important: Instances of ABAddressBookRef cannot be used by multiple threads. Each thread must make its own instance by calling ABAddressBookCreate.
你可以做的是创建一个ABAddressBook的一个实例,并创建一个生产者/消费者体系结构来管理对象的访问。
What you can do is create a single instance of the ABAddressBook and create a producer/consumer architecture that would manage the access to the object.
包装器将有一个主线程只做一件事:读操作来自阻塞队列的请求,然后在地址簿上执行操作。所有线程都将其操作排入单个队列,包装器将执行这些操作;如果队列中没有任何内容,则包装器将阻塞,直到队列中有东西为止。
The wrapper will have a main thread which does one thing only: reads operation requests from a blocking queue, then performs the operations on the address book. All your threads will queue their operations to the single queue and the wrapper will execute those actions; if there is nothing in the queue then the wrapper will block until there is something in the queue.
这应解决不允许使用<$ c的问题来自多个线程的$ c> ABAddressBookRef 。
这篇关于iPhone多线程AddressBook操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!