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.
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 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 :
A sample Deadlock Code :
static object a = new object();
static object b =new object();
public static void lock1()
Console.WriteLine("Waiting for Resource B");
Console.WriteLine("Accquired Resouce B");
public static void lock2()
Console.WriteLine("Waiting for Resource A");
Console.WriteLine("Accquired Resouce A");
public static void CreateThreads()
var thread = new Thread;
thread = new Thread(lock1);
thread = new Thread(lock2);
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.
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.
//It is equivalent to Monitor.Enter and Monitor.Exit.
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.
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).
The web API processing architecture can be divided into three Layers.
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).
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.
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 :
Route Dispatcher :
This is essentially where we declare the routes of web API.(Routeconfig in MVC/WEB API).
- 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.
- 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.
The last and final layer where we write the concrete business logic methods.This layer includes functionalities like
Over All Architecture of WEB API :