Sunday, September 11, 2011

Thread Synchronization

Que: What are different mechanism provided by c# for thread synchronization?
Ans: 
Monitor:
Static class available in System.Threading namespace
Method available: Enter/Try Enter, Wait, Pulse/PulseAll,Exit
Code between Monitor.Enter and Monitor.Exit is considered in synch block.
It has some disadvantages like the code can end up in deadlock also if we don't use Enter and Exit method cleverly. That is why we prefer to use lock statement instead.

ReadWriterLock:
Available in System.Threading namespace.
It is generally used to secure shared resources for writing and reading. Main disadvantage of this is Reading thread is given preference over writing. Thus sometimes there is performance hit on writing method.

WaitHandle:
Available in System.Threading namespace.It is Abstract class.
Mutex,Semaphore and EventWaitHandle(AutoResetEvent and ManualResetEvent) are classes that are derived from this abstract class. Semaphore is available in System.dll and others are in MSCoreLib.Dll.

AutoResetEvent Class:
This class notifies one or more waiting threads that an event has occurred. It automatically changes the status to signaled when a waiting thread is released.This class cannot be inherited.

ManualResetEvent Class
This class also notifies one or more waiting threads that an event has occurred. The state of the event can be manually set or reset. The state of a manually reset event remains signaled until the ManualResetEvent.Reset method sets it to the nonsignaled state and the state remains nonsignaled until the ManualResetEvent.Set method changes the state back to signaled. This class cannot be inherited.

Interlocked Class
This class (in the System.Threading namespace) helps to synchronize access to variables that are shared amongst threads. It thus provides atomic operations for variables that are shared by multiple threads. You can increment or decrement a shared variable by calling Interlocked.Increment or Interlocked.Decrement on the shared variable.

Que: What is the advantage of Waithandle class over Monitor/ReadWriteLock?
Ans: Monitor and ReadWriteLock methods allow synchronization of thread in a single appdomain, whereas being a Kernel level Waithandle does allow to have thread synch in different Appdomain and different process also.

Que: Effect of passing value type in monitor:
Ans: Unboxed value type instance do not have a sync block index member therefore, they can not be used for synchronization. That is why when we pass Value type instance in Monitor.Enter c# compiler will generate code to box the instance.And if we pass the same value in Monitor.Exit,it gets boxed again. Our code endup locking one object and unlocking another. and we dont get any thread safety. In case of lock statement compiler will generate error saying valuetype not a reference type as required by lock statement.

Que: How to apply lock in static class?
Ans: We can use lock(typeOf(Class_Name)).

Que: What is difference between Mutex and Semaphore?
Ans: In Mutex we can have only one thread accessing the object whereas with Semaphore we can have more than one thread(upto certain max limit) accessing the object simultaneously.

No comments:

Post a Comment