关于iPhone上的多线程
我有一个问题。我的案例研究是我有两个大型 SQLite 数据库,并且我想使用线程(意味着同时有 2 个进程)。效果好吗?我写了以下代码:
NSAutoreleasePool *dbPool;
dbPool = [[NSAutoreleasePool alloc] init];
/* All Database work is performed here */
[dbPool release];
请指导我。我这样做正确与否?我应该使用水池排水还是释放?
这样,这是否使用并发进程,这意味着它也与多任务处理相同的行为?
提前致谢!
I have a question. My case study is that I have two big SQLite databases and I want to use threads (meaning 2 processes simultaneously). Did it work well? I have written the following code:
NSAutoreleasePool *dbPool;
dbPool = [[NSAutoreleasePool alloc] init];
/* All Database work is performed here */
[dbPool release];
Please guide me. Am I doing this correctly or not? Should I use a pool to drain or release?
And in that way is this using concurrent processes, meaning it's also the same behavior as multitasking?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你做得对。每个新线程都需要自己的自动释放池。
关于您有关池释放/排空的问题,建议使用排空消息。
sqlite数据库是什么意思?你如何访问它?如果你通过 CoreData 访问它,你会一直记住:
还有更多规则,但这些是基本规则。
多任务处理意味着您可以一次运行更多应用程序。多线程(=您的情况)意味着您的应用程序确实使用更多线程来完成其任务。
Yep, you're doing it right. Each of your new thread needs own autorelease pool.
Regarding to your question about release / drain of pool, recommended is drain message.
What do you mean by sqlite database? How do you access it? If you access it via CoreData, you have keep following in your memory:
There are more rules, but these are basic ones.
Multitasking means that you can run more applications in one time. Multithreading (= your case) means that your application does use more threads to achieve its task.
用户界面或其他繁重对象管理工作的常见方法是像您正在做的那样包围您的代码,但您应该使用
drain
:关于
NSAutoreleasePool
的大量详细信息可以在此处 以及之前的 Stack Overflow 答案此处。基本上,如果设置为autorelease
,您在池内所做的工作将在池耗尽后释放。当使用某些生成自动释放实例的类时,这可以提高性能。如果您想要完全且立即的控制,您可以在不再需要时释放
您正在使用的每个对象,并完全放弃池。至于您的多线程问题,我不确定我是否理解您的要求,但即使在后台线程中,使用池也是一种可靠的方法。这是假设您在线程中使用的对象不会以某种方式在另一个线程中使用(因为您可能会意外释放)。
A common approach for user interface or other heavy object management work is to surround your code like you're doing, but you should be using
drain
:A lot of detail on
NSAutoreleasePool
is available here and a previous Stack Overflow answer here. Basically the work you're doing inside the pool, if set toautorelease
, will be released once the pool drains. This can increase performance when working with certain classes that produce autoreleased instances. If you want full and immediate control though, you canrelease
each object you're working with once it's no longer needed and ditch the pool altogether.As to your multithreading questions I'm not sure if I understand what you're asking but nonetheless using the pool is a solid approach even in a background thread. This is assuming that the objects you're working with in the thread aren't somehow also used in another (since you might have an accidental release).