Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Usage of Lock statement in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 298
    Comment on it

    Usage of Lock statement in C#


    Lock keyword in C# find it's usage in Threading. Whenever multiple threads try to access some code at same time inconsistency occurs then lock keyword is used which makes execution of only one thread 
    at any point of time. With lock keyword reliability is achieved in threaded programs.

     

    Example without using Lock statement in C# :-

     

    using System;
    using System.Threading;
    
    namespace DemoApplication
    {
        class Program
        {
            static Object obj = new Object();
            static void Main(string[] args)
            {
                new Thread(starPattern).Start();
                new Thread(starPattern).Start();
                Console.ReadKey();
            }
    
            static void starPattern()
            {
              for (int i = 0; i < 5; i++)
              Console.Write("*" + "\t");
              Console.WriteLine("+");
            }    
        }
    }

     

    The output will differ each time application is run since two threads are accessing same code at same time,following are some outputs :-

     

    Output 1:

     

     

     

    Output 2 :

     

     

    Example to demonstrate Lock keyword in C# :

     

    using System;
    using System.Threading;
    
    namespace DemoApplication
    {
        class Program
        {
            static Object obj = new Object();
            static void Main(string[] args)
            {
                new Thread(starPattern).Start();
                new Thread(starPattern).Start();
                Console.ReadKey();
            }
    
            static void starPattern()
            {
                lock (obj)    // Thread safe code
                {
                    for (int i = 0; i < 5; i++)
                    Console.Write("*" + "\t");
                }
              Console.WriteLine("+");
            }    
        }
    }

     

    Output :

     

     

    In above code two threads are created which are calling the same method therefore giving rise to inconsistency. By using lock keyword first thread enters critical section and it will lock object obj and when other thread tries to enter the same critical section then it will also try to lock obj which is in turn is being locked by first thread, the second thread needs to wait for first thread to release obj and whenever first thread will leave critical section and free up obj then other thread waiting in queue will lock obj and will enter to critical section for execution.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: