哪个是GCD,NSThread或NSOperationQueue中最好的? [英] Which is the best of GCD, NSThread or NSOperationQueue?

查看:109
本文介绍了哪个是GCD,NSThread或NSOperationQueue中最好的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS中多线程的最佳方式是什么,因为我们有三个选项GCD, NSThread NSOperationQueue ?我很困惑哪一个是最好的?如果没有,那么应该在哪种情况下使用它们以及它们如何不同,如果有人有一些使用 NSOperationQueue 的好例子,请分享以便我可以学习。

What's the best way of multithreading in iOS as we have three options GCD, NSThread, and NSOperationQueue? I am confused in which one is the best? If none, then which should be used in what scenario and how they differ and also, if someone has some good example of using NSOperationQueue, please share so that I can learn.

推荐答案

简单回答:


  1. 当您需要或需要直接控制您创建的线程时,请使用NSThread(甚至是pthreads API),例如您需要对线程优先级进行细粒度控制,或者与直接销售/使用线程对象的其他子系统连接,您需要与它保持同一页面。这种情况很少见,但它们确实存在,特别是在实时应用程序中。

  1. Use NSThread (or even the pthreads API) when you want or need to have direct control over the threads you create, e.g. you need fine-grained control over thread priorities or are interfacing with some other subsystem that vends/consumes thread objects directly and you need to stay on the same page with it. Such instances are rare, but they do occur, particularly in real-time applications.

当您的任务非常适合简单的并行化时使用GCD,例如:你只想把一些工作投入到后台而只需要很少的额外工作,你有一些数据结构你只想序列化访问(并且串行队列非常适合以无锁方式进行),你有一些用于循环可以很好地与dispatch_apply()进行并行化,你有一些数据源/定时器,GCD的源API将使你能够在后台轻松处理等等.GCD非常强大,你可以使用它很多更重要的是,这些都是相对没有脑子的情况,你不想陷入初始化和设置任务,就像简单地并行做基本的东西一样。

Use GCD when your task lends itself well to simple parallelization, e.g. you just want to toss some work "into the background" with very little additional work, you have some data structures that you merely wish to serialize access to (and serial queues are great for doing that in a lockless fashion), you have some for loops that would lend themselves well to parallelization with dispatch_apply(), you have some data sources / timers that GCD's sources API will enable you to deal with easily in the background, etc etc. GCD is quite powerful and you can use it for a lot more than this, but these are all relative 'no brainer' scenarios where you don't want to get caught up in the initialization and setup tasks so much as simply "do basic stuff in parallel".

当您已经进入Cocoa API层时(使用直接C写入POSIX API)并使用更复杂的操作进行并行化时,请使用NSOperation。 NSOperation允许子类化,任意复杂的依赖图,取消和支持许多其他可能对您有用的更高级语义。 NSOperation实际上使用了GCD,所以它就像GCD一样多核,多线程,虽然它也带来了基础框架,所以如果你正在攻击POSIX层,你可能想要使用它选项#2。

Use NSOperation when you're already up at the Cocoa API layer (vs writing in straight C to the POSIX APIs) and have more complex operations you want to parallelize. NSOperation allows for subclassing, arbitrarily complex dependency graphs, cancellation and a supports a number of other higher-level semantics that may be useful to you. NSOperation actually uses GCD under the covers so it's every bit as multi-core, multi-thread capable as GCD, though it also brings the Foundation framework along for the ride so if you're hacking at the POSIX layer, you probably want to use option #2.

然而,正如其他人所说,这一切都取决于你想要做什么这样的对你的问题没有单一甚至普遍正确的答案。

As others have said, however, it all depends on What You Are Trying To Do so there's no single or even universally correct answer to your question.

这篇关于哪个是GCD,NSThread或NSOperationQueue中最好的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆