Friday, November 22, 2013

What is @synchronized directive in Objective C?

In a multithreaded environment if more than one thread tries to access same memory address may cause a Race Condition”, to avoid such kind of conditions you should use “Mutex Lock(Mutual Exclusion)” nothing but blocking or restricting or locking n number of threads to access same memory address or content at a same point of time and allowing only one thread at an instance of time.
This can be achieved in Objective C by using @synchronized directive.

Generally while implementing Singleton design pattern or class you will see some kind of code snippet like below in any iOS projects,

        if (singletonObj == nil)
            singletonObj = [[self alloc] init];
        return singletonObj;

In the above example if suppose at a particular instance of time, if two different class tries to call getSingletonInstance method then if you don’t manage thread safety then a Race Condition will occur, so as to avoid such kind of exceptional conditions you need to use @synchronized directive.
Here whenever a thread reaches this line of code, @synchronized directive will check whether any other thread is already accessing self, if some thread is already accessing it will just block the current thread until and unless the processing thread will finishes its task.

At this point you might be thinking that why do we need to use threads? & why we want to manage threads?
Here is the answer,
- Basically a thread is an instance of a Process, A process may have n number of threads (we can say threads are children’s of a process)

why do we need to use threads?
In Software Programming we need to take care of CPU resources optimizations so that the performance of our application can be increased. If we consider any iOS application basically there will be a main thread for each application where exactly the app starts and all the UI manipulations has been taken care by this main thread.

In this kind of environment suppose say an example, if you are downloading an image from a remote server through a web-service (ex: REST) call, then if you perform this task on main thread then until and unless you get the response from the server your UI will become hang, so always this kind of asynchronous tasks should be handled by separate threads so as to utilize CPU, system resources properly and to increase your apps performance.

why we want to manage threads?
- Every thread of a process has equal access to your application resources to fetch, modify or manipulate.

- Each thread will not make sure that how long it will run and when it will finishes its task.
So it’s a programmer's responsibility or the one who creates a thread its his/her responsibility to manage threads.

Advantages of using multiple threads
- Use of multiple threads will reduce CPU waiting time.

For additional information on synchronization you can refer Apple’s official documentation in following URL

Hope this is helpful, any comments or suggestions are acceptable.