#AutoResetEvent Vs ManualReset Event

Scenario : Lets assume there are two threads waiting to access a resource/ CS.We are using Signaling mechanism.

AutoResetEvent : Only one thread out of the two will be allowed,once signal is received.

ManualResetEvent : All the threads which have been in halt will be allowed once signal is received. To prevent this you must invoke a method called Reset Manually.

Rule of Thumb: For every waitone,there must be a corresponding set.

 

Untitled

 

#Signaling Mechanism- WaitHandle Abstract Class

WaitHandle :

1.This class is responsible for synchronization using signal mechanism. It is responsible for wrapping kernel Handle Object.

2.Mutex,Semaphore,AutoResetEvent,ManualResetEvent all inherits from WaitHandle.

Important Methods  in WaitHandle :

1.WaitOne : Thread gets blocked if its see waitone method.Thread goes to halt and it will remain in halt unless it receives a signal.Expiration for waiting period can be set with timer option.

2.WaitAll : Collection of WaitHandler method.It is used when all of the items in waithandler array has recived a signal(useful to prevent deadlock ) .

3.WaitAny:Similar to WaitAll,Its a collection of waitHandler  but it waits only for any one of the item in the array to be signalled.

 

 

#Mutex :Kernel level Synchronization Mechanism

Mutex Uses :

1.used when we need deadlock free Multiple Lock Acquisition. Mutex implemented with help of WaitHandle.

2.Single Instance of Application Instance.

Dead Lock free Multiple Lock Acquisition :

Capture.PNG

A sample Deadlock Code :


class Deadlock
{
static object a = new object();
static object b =new object();

public static void lock1()
{
lock (a)
{
Thread.Sleep(1000);
Console.WriteLine("Waiting for Resource B");
lock (b)
{
Console.WriteLine("Accquired Resouce B");
}
}

}

public static void lock2()
{
lock (b)
{
Thread.Sleep(1000);
Console.WriteLine("Waiting for Resource A");
lock (a)
{
Console.WriteLine("Accquired Resouce A");
}
}

}

public static void CreateThreads()
{
var thread = new Thread[2];
thread[0] = new Thread(lock1);
thread[1] = new Thread(lock2);
thread[0].Start();
thread[1].Start();
}

}

Mutex and WaitHandle : 

We could  use  WaitHandle Method WaitHandle.WaitAll() which takes parameter as an array of mutex(mutex inherits from waithandle) which would return true or false based on resource availability. 

#Wait based Synchronization Mechanism-Monitor.

Monitors : Monitor is one of  gated synchronization mechanism which consist of two methods which guarantees thread safe behavior.

1.Monitor.Enter : Threads that enter critical section informs other threads  that it had acquired the resource/CS and no more threads can take ownership on the same and other threads requiring the resource/CS must wait.

2.Monitor.Exit :The thread which had acquired resource/CS  releases the ownership on thread.

In C#,Monitors equivalent code is lock.


lock(object)

{

//CS

}

//It is equivalent to Monitor.Enter and Monitor.Exit.

try

{

Monitor.Enter(object)

{

//CS

}

}

Finally()

{

Monitor.Exit(object)

}

 

 

Capture

 

#Interfaces VS Abstract Class

Abstract class gives you all the benefits that interfaces does,except very  important one feature “Multiple Inheritance“. This is the sole reason why we are asked to write “program to an interface”.You would argue that i would load a constructor with a abstract class rather than interface,but it would fail since you cannot implement more than one class and you need interface to do that.Capture

#Web API Processing Architechture

Over View :

In this article we will discuss the behavior of  web api when a request is made(Request) and an output it produces(Response).

Architecture :

The web API processing architecture can be divided into three Layers.

1.Hosting Layer.

2.Message Handler Pipeline Layer.

3.Controller Handler Layer.

Hosting Layer :

This layer that is responsible for exposing the web API to outside world.The Hosting layer takes care of bridging the web API and HTTP infrastructure(HTTP Listener).

Responsibilities :

1.It is also responsible for  creating HTTPRequestMessgae when HTTP Request is made and then pushing the request to next layer(Message Handler)

2.It is also responsible for converting the HTTPResponseMessage received from the Message Handler and then sending it as HTTP Response Message.

Capture.PNG

Mesaage Handler Layer:

A Message Handler acts a middle layer that performs operation over HTTPRequestMessage and HTTPResponseMessage.

For instance,when a response header  is sent from the server,you would find a attribute named “Server” which contains the server name where the request was processed. All these kind of operation are done over this layer.

There are two important  handlers in Message Handler :

1.Route Dispatcher.

2.Controller Dispatcher.

Route Dispatcher :

This is essentially where we declare the routes of web API.(Routeconfig in MVC/WEB API).

Responsibilities :

  1. When a route is made and if it has the matching route defined in Route Config ,then the Route Dispatcher forwards it to the Controller Dispatcher.
  2. If there are no match in Route,Return 404.

Controller Dispatcher :

The Controller Dispatcher is responsible for calling the respective controllers in Web API. This Dispatcher acts as a bridge between Message Handler and Controller Handler. 

ControllerHandler :

The last and final layer where we write the concrete business logic methods.This layer includes functionalities like

1.Authorization,Authentication.

2.Expection Filters.

3.Action Filters.

4.Model Binding.

Over All Architecture of WEB  API :

[Diagram]