Wednesday, May 15, 2013

Synchronous Threading

Now once again, it was Pollard who was batting. But this time, I did watched him. I did a congrats tweet to him too before writing this code for the post. Well this post is basically going to let know how one can make threads run Synchronously.
There are times in every one's life where they need to do Sync operation via two or more threads. Before going further one may ask what is a Sync operation? So before they raise this question, here is the jest of what it actually is.

Synchronous Operations

The best real world example, that I can think of to explain the Sync operation is when you are cooking. Imagine you are cooking Shaahi Paneer(the most tasty Indian Dish, I just love it). Also imagine that you have a gas stove with 4 burners. Now the recipe of the dish says:

1. First you need to cut the cheese(paneer) into small pieces.

2. Put a frying pan with oil on a burner to get hot.

3. Put the cheese’ pieces into the pan and fry them till they are fried properly.

4. Once they are fried, put them into the gravy that have been prepared before(how to prepare gravy is out of the scope of this post).

Now as you might observed, step 1 and step 2 might go side by side. That is to say, you can do both the operation of cutting the cheese and warming the pan simultaneously. These are called Asynchronous Operations, that is both can go on and on parallel, having nothing to do with one another.
But for the step 3 of course, you need both task of step 1 and step 2 complete. You cannot proceed frying the cheese until the oil is warm and cheese has been cut into pieces. The task 3 therefore, can be scribbled as the Synchronous task. You need to stop until both the above steps finishes.
Similarly you cannot proceed into step 4 until you are done with step 3. This step 4 therefore is a synchronous operation.

How it all looks, in code?

Just for the sake of brevity, let us assume that all the above four steps are carried out by four different threads.
In order to stop the execution of the threads, we need a class AutoResetEvent.

   1: readonly AutoResetEvent m_autoreset = new AutoResetEvent(false);

Here is the entire code for my above Cheese Program:

   1: class Program
   2: {
   3:     readonly AutoResetEvent m_autoreset = new AutoResetEvent(false);
   4:     static void Main(string[] args)
   5:     {
   6:         var me = new Program();
   7:  
   8:         var cutCheese = new Thread(me.CutCheese);
   9:         var warmOil = new Thread(me.WarmOil);
  10:         var frCheese = new Thread(me.FrCheese);
  11:         var addToGravy = new Thread(me.AddToGravy);
  12:  
  13:         cutCheese.Start();
  14:         warmOil.Start();
  15:         //Wait for above process to complete
  16:         me.m_autoreset.WaitOne();
  17:         frCheese.Start();
  18:         me.m_autoreset.WaitOne();
  19:         addToGravy.Start();
  20:  
  21:     }
  22:  
  23:     private void CutCheese()
  24:     {
  25:         
  26:     }
  27:     private void WarmOil()
  28:     {
  29:         var me = new Program();
  30:         //Reset the wait process
  31:         me.m_autoreset.Set();
  32:     }
  33:     private void FrCheese()
  34:     {
  35:         var me = new Program();
  36:         //Reset the wait process
  37:         me.m_autoreset.Set();
  38:     }
  39:     private void AddToGravy()
  40:     {
  41:  
  42:     }
  43: }

The line me.m_autoreset.WaitOne(); will stop the execution of the code and keep it there until me.m_autoreset.Set(); is called. The two threads, cutCheese and warmOil will start and further execution will be blocked until both are complete. Once done, the execution will resume, and henceforth.

The process, therefore suggests that you can even make the Async process to go Sync. And at the end of the day, you will get a very good dish to eat.

Happy reading!!!

Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© 2013 Neelesh Vishwakarma
Posts RSS Comments RSS
Back to top