[英]Difference between operations & threads in Cocoa
I am learning Objective-C, I will be developing a video processing application. 我正在学习Objective-C,我将开发一个视频处理应用程序。
I am reading the developer's guide on apple and I ran into Operation objects. 我正在阅读有关Apple的开发人员指南,并且遇到了Operation对象。 I just need some clarifications what the difference between operations & threads, disadvantages, advantages, use cases.
我只需要澄清操作和线程,缺点,优点,用例之间的区别。
From what I read, operation is higher level of application process/task management. 据我了解,操作是更高级别的应用程序流程/任务管理。 The NSOperationQueue can handle operation resources and concurrency.
NSOperationQueue可以处理操作资源和并发性。
What I don't understand is that Operation can be directly run in a thread, I am not too clear on this concept. 我不了解的是Operation可以直接在线程中运行,我对此概念不太清楚。 I am not to clear on the difference between operations & threads.
我不清楚操作和线程之间的区别。
Please provide me with as much information/background on the two as possible. 请向我提供有关这两者的尽可能多的信息/背景。
Thanks a lot everyone. 非常感谢大家。
The difference between NSOperation
and threads is one of abstraction. NSOperation
和线程之间的区别是抽象之一。 A thread is a low-level (operating system-level) construct with which to execute multiple "threads" of code concurrently. 线程是低级(操作系统级)结构,可同时执行多个“线程”代码。 Although Cocoa provides the
NSThread
API, it is essentially a wrapper on the pthread
s API. 尽管Cocoa提供了
NSThread
API,但它实际上是pthread
s API的包装。 NSOperation
is a higher-level abstraction of a task that you wish to execute. NSOperation
是您希望执行的任务的高层抽象。 NSOperationQueue
will schedule execution of a queue of NSOperations
so as to maximally utilize the available CPU(s). NSOperationQueue
将安排执行NSOperations
队列,以便最大程度地利用可用的CPU。 On a multi-core system, multiple NSOperations
will be executed simultaneously using a pool of threads that NSOperationQueue
maintains. 在多核系统上,将使用
NSOperationQueue
维护的线程池同时执行多个NSOperations
。 The advantage of using this higher-level API is that it lets you think about the "operations" you wish to perform rather than how to schedule them. 使用此更高级别的API的优点在于,它使您可以考虑要执行的“操作”,而不是如何安排它们。 The disadvantage (and hence the advantage of using
NSThread
directly) is that you have more control over the scheduling of the thread and communication between the thread and other threads (see -[NSObject performSelector:withObject:onThread:]
). 缺点(以及直接使用
NSThread
的优点)在于,您可以更好地控制线程的调度以及该线程与其他线程之间的通信(请参见-[NSObject performSelector:withObject:onThread:]
)。 For atomic tasks, such as video processing, NSOperation
is likely the best fit. 对于原子任务,例如视频处理,
NSOperation
可能是最合适的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.