关于iPhone上的多线程

发布于 2024-10-27 11:40:40 字数 325 浏览 9 评论 0原文

我有一个问题。我的案例研究是我有两个大型 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一笑 2024-11-03 11:40:40

是的,你做得对。每个新线程都需要自己的自动释放池。

关于您有关池释放/排空的问题,建议使用排空消息。

sqlite数据库是什么意思?你如何访问它?如果你通过 CoreData 访问它,你会一直记住:

  • 每个线程需要一个 NSManagedObjectContext,
  • 不要将 NSManagedObjects 传递给另一个线程,只需传递对象 ID,
  • 在将对象 ID 传递给另一个线程之前,将其保存在它所在的线程中在传递之前被修改/创建。

还有更多规则,但这些是基本规则。

多任务处理意味着您可以一次运行更多应用程序。多线程(=您的情况)意味着您的应用程序确实使用更多线程来完成其任务。

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:

  • you need one NSManagedObjectContext per thread,
  • do not pass NSManagedObjects to another thread, just pass object ID,
  • before you pass object ID to another thread, save it in thread where it was modified / created before passing it.

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.

最冷一天 2024-11-03 11:40:40

用户界面或其他繁重对象管理工作的常见方法是像您正在做的那样包围您的代码,但您应该使用 drain

NSAutoreleasePool *dbPool = [[NSAutoreleasePool alloc] init];

// do your work

[dbPool 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:

NSAutoreleasePool *dbPool = [[NSAutoreleasePool alloc] init];

// do your work

[dbPool 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 to autorelease, 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 can release 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文