Pages

Grand Central Dispatch(GCD) Basics

What Is Grand Central Dispatch?

        GCD is basically all about Blocks and Queues. GCD is a very low level C based Abstract Programming Interface(API) with the help of which we can perform concurrent programming. It's basic functionality is pretty much like NSOperationQueue, by using GCD API developers can divide their program into individual tasks which are then submitted to queues which can execute these tasks concurrently or serially as specified. GCD is lower level and higher performance API than NSOperationQueue, and again it is not a part of the Cocoa frameworks.

        Apart from the facility of parallel execution of code(individual tasks), GCD also provides a facility known as event handling system. In event handling system Handlers can be set up to respond to events on file descriptors, mach ports, and processes, to timers and signals which is pretty much like a facility of signal handling in C and to user-generated events. These handlers or functions which handle these events are executed through the GCD facilities for concurrent execution.
        We can also use GCD without blocks, other facility by which GCD can be used is by using the traditional C mechanism of providing a function pointer and a context pointer, this facility a very easier to use and much capable, as I will show you,when used with blocks.

What makes it better?
Here are some of features that makes it  a better API than others.

1.) GCD manages threads for you, when needed creates for you if not needed dispose threads for later use. It manages thread pool which initially contains threads. Threads in thread pool are number of cores(CPU) in your device.

2.) It takes your burden of waiting for a task to finish, suspending tasks, manage file descriptors. And since it is a block based API it makes it easier to pass context from in block to another.

3.) It is very light weighted API. Sometimes creating threads and managing them is very difficult. You use it just like that without worrying about efficiency. It works efficiently on your behalf.

Grand Central Dispatch Uses:

1.) Dispatch Objects.
2.) Dispatch Queues.
3.) Quality of service classes(QoS).

A.) Main Dispatch Objects are:

1.) dispatch_main()   --> Parks Main thread and wait for task to submitted on Main Queue.
2.) dispatch_sync()    --> Performs synchronous task i.e. replaces Locks.
3.) dispatch_async()  --> Performs asynchronous task i.e. generates another thread of task to complete
4.) dispatch_retain()  --> Manages auto release pool.
5.) dispatch_release()  --> Manages auto release pool.
6.) dispatch_groups()  --> Create and manage groups of tasks that you submit.
7.) dispatch_semaphores()  --> Behaves as Sync primitives good for debugging. 
8.) dispatch_barriers()  --> Create a synchronization point int a group of async tasks.
9.) dispatch_sources()  --> Interface for monitoring file descriptors, signals, ports, io.
10.) dispatch_block()  --> Creates a block (task) for you to submit in queue.

B.)  dispatch queues are of type:

1.) dispatch_get_main_queue()  --> returns main queue associated with your thread.
2.) dispacth_get_global_queue()  --> returns a global concurrent queue.  
3.) dispatch_queue_create()  --> creates a serial queue.

C.) Quality of service classes are:

1.) QOS_CLASS_USER_INTRACTIVE  --> Flag indicates you want to use User interactive class.
2.) QOS_CLASS_USER_INITIATIVE --> Flag indicates you want to use User initiative class.
3.) QOS_CLASS_UTILITY  --> Flag indicates you want to use Utility class.
4.) QOS_CLASS_BACKGROUND --> Flag indicates you want to use Background class.

        This was the basic description about GCD API. It is a complete series of tutorial. If you want to dive into this API then follow this series, will contains more interesting examples.



No comments:

Post a Comment