This chapter has covered a variety of approaches for concurrent programming, so you have several options to choose from. To recap, the following are the available options:
Asynchronous APIs. The Foundation Framework (along with the Cocoa and Cocoa Touch frameworks) includes a variety of APIs that perform asynchronous processing; examples include the NSURLConnection, NSFileHandle, andNSPort classes.
Threads. APIs that use threads to implement concurrent programming. This includes the Foundation Framework message passing APIs, theNSThread class, and the synchronization mechanisms.
Operation queues. NSOperation andNSOperationQueue are Objective-C APIs that can be used to implement concurrent programming using queues.
Dispatch queues. Grand Central Dispatch is a C-based API and set of services that can be used to implement concurrent programming using dispatch queues.
1. In general, you should use the asynchronous APIs to implement asynchronous/concurrent processing if possible. These APIs use various technologies (threads, queues, etc.) to provide concurrency that scales with the capabilities of the system and enable your program design to align with the program style and capabilities of the Objective-C platform.
Although the Objective-C platform provides a number of language features and APIs to support thread-based concurrent programming, 2.threads are not the recommendation approach for concurrent programming. Operation queues and dispatch queues are the preferred mechanism for asynchronous, concurrent processing.These should be used to concurrently perform tasks that are not supported by the asynchronous APIs, such as executing a long computation, background data processing, and so forth.
Operation and dispatch queues provide an asynchronous, queue-based approach that eliminate the need for low-level thread management and maximize system utilization and efficiency compared to thread-based programming. 3.Operation queues, being object-based, have more overhead and utilize more resources than GCD dispatch queues. However, the higher-level, object-oriented API is consistent with the Objective-C platform and may be easier to use. In addition, operation queues provide support for complex interoperation dependencies, constraint-based execution, and management of operation objects.
4. GCD, as it provides a lower-level (C-based) API, is lightweight and provides better performance than operation queues. As shown by the example ConcurrentDispatch program, the GCD block-based approach can result in fewer lines of code that may minimize overall program complexity.
Finally, as operation and dispatch queues do not address real-time constraints, 5. threads are still an appropriate mechanism for concurrent programming of real-time systems.