Reputation: 1831
I am learning Objective-C, I will be developing a video processing application.
I am reading the developer's guide on apple and I ran into Operation objects. 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.
What I don't understand is that Operation can be directly run in a thread, I am not too clear on this concept. 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.
Upvotes: 0
Views: 556
Reputation: 107754
The difference between NSOperation
and threads is one of abstraction. 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. NSOperation
is a higher-level abstraction of a task that you wish to execute. NSOperationQueue
will schedule execution of a queue of NSOperations
so as to maximally utilize the available CPU(s). On a multi-core system, multiple NSOperations
will be executed simultaneously using a pool of threads that NSOperationQueue
maintains. 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. 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:]
). For atomic tasks, such as video processing, NSOperation
is likely the best fit.
Upvotes: 2