Introduction to GCD Grand Central Dispatch

Introduction to GCD Grand Central Dispatch we will try to cover the basic in this tutorial. There is numerous ways to use this in your programme.

     GCD Grand Central Dispatch is a low level C based Abstract Programming Interface(API) with the help of which we can perform concurrent programming. It’s basic functionality is pretty similar to NSOperationQueue, by using GCD API we can divide big task into individual tasks which are then submitted to queues which can execute concurrently or serially as specified. GCD Grand Central Dispatch is higher performance API than NSOperationQueue.

        Apart from Parallel execution of tasks, GCD also provides a functionality 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 similar to signal handling in C and to user-generated events. These handlers or functions are executed through the GCD facilities for concurrent execution.
        We can also use GCD without blocks and can be used like the traditional C mechanism of providing a function pointer and a context pointer, it is very easier to use and much capable.
Introduction to GCD Grand Central Dispatch
What makes GCD Grand Central Dispatch better ?
1.) GCD manages threads for you, it create threads when required or dispose for later . 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 compilation, 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. Using this creating threads and managing is so simple. They can be used so easily and efficiently.
Grand Central Dispatch :
 
A : Dispatch Objects.
B :  Dispatch Queues.

C :  Quality of service classes(QoS).

Running a task in background :

1.) dispatch_sync() is used for synchronization purpose in your application. It blocks thread on which you use it until block you submit to this function will successfully complete its execution. This function performs lock on the thread you use it on.

Here are two implementations of dispatch_sync():

Above function is just basic demonstrate how dispatch_sync() works. Here what is happening is, you passed main queueas an argument to dispatch_sync() function which is like you are executing this function on main thread. So this will block your main thread until the task you submitted will complete it’s execution. It means this function will not return immediately. Of course there are a lot more ways to use this function, on different threads that is. for example:

If you use dispatch_sync() function this way this implementation won’t block you main thread but synchronize the task on the global concurrent queue. Means if global queue has five task in queue and dispatch_sync() is any of them, since dispatch_get_global_queue() function returns a global concurrent queue which execute each task concurrently irrespective to other then all five will execute concurrently without block anything.

2.) dispatch_async() is used for performing a task in background means executing a task on a different thread than main thread. It won’t blocks any thread but return immediately after starting a task. This function creates a thread for the task you submit to it and schedule it for execution.

Here is implementation of dispatch_async():

Above implementation of dispatch_async() function will create a thread for your task to execute and then return immediately then you task will start it’s execution.
Now if you want to update your app’s User Interface after successful completion of background task then you can do this by applying nested dispatch blocks. Here is a particular example .

The above implementation is of nested dispatch blocks in which after execution of task in background you can update user interface by placing your code in dispatch_async(dispatch_get_main_queue()) block.
As the same way you can nest dispatch_async() and dispatch_sync() blocks.

Today we have a little Introduction to GCD Grand Central Dispatch. There is a lot more to explore. Happy coding 🙂

 

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.