C# Lock (Thread Lock)

In c#, the lock keyword is useful to acquire the mutual-exclusion of lock for the specified block of code to make sure that at a time, only one thread can execute it.

 

If any other thread wants to execute the same piece of code, then it should wait until the thread that holds the lock finishes its execution.

 

Generally, the lock keyword is useful while working with multithreading applications to make sure that at a time, the only thread can access the particular piece of code.

C# Lock Syntax

Following is the syntax of using lock keyword in c# to acquire the mutual-exclusion of lock for the specified piece of code.

 

lock (x)
{
// Your code
}

Here, x is an expression of reference type, and within the lock statement, we need to write the code which we want to allow access for one thread at a time.

C# Thread Lock Example

Following is the example of using lock keyword in c# to lock the particular piece of code to allow execution of one thread at a time.

 

using System;
using System.Threading;

namespace TutlaneExamples
{
    class Program
    {
       static readonly object pblock = new object();
       static void PrintInfo()
       {
          lock (pblock)
          {
             for (int i = 1; i <= 4; i++)
             {
                Console.WriteLine("i value: {0}", i);
                Thread.Sleep(1000);
             }
          }
       }
       static void Main(string[] args)
       {
          Thread t1 = new Thread(new ThreadStart(PrintInfo));
          Thread t2 = new Thread(new ThreadStart(PrintInfo));
          t1.Start();
          t2.Start();
          Console.ReadLine();
       }
    }
}

If you observe the above example, we created multiple threads (t1 & t2) and starting to execute the PrintInfo method simultaneously. Here, in the PrintInfo method, we used lock statement to acquire the mutual-exclusion of lock for the for loop code execution to allow only one thread execution at a time.

 

When we execute the above result, we will get the result as shown below.

 

i value: 1
i value: 2
i value: 3
i value: 4
i value: 1
i value: 2
i value: 3
i value: 4

If you observe the result, even if we start the multiple threads simultaneously to execute the same functionality, the lock statement made the second thread wait until the first thread finishes its execution.

C# Thread without Lock Example

In the above example, if we didn’t use the lock statement, then the multiple threads will execute simultaneously.

 

Following is the example of multithreading in c# without using lock statement.

 

using System;
using System.Threading;

namespace TutlaneExamples
{
   class Program
   {
      static void PrintInfo()
      {
         for (int i = 1; i <= 4; i++)
         {
            Console.WriteLine("i value: {0}", i);
            Thread.Sleep(1000);
         }
      }
      static void Main(string[] args)
      {
         Thread t1 = new Thread(new ThreadStart(PrintInfo));
         Thread t2 = new Thread(new ThreadStart(PrintInfo));
         t1.Start();
         t2.Start();
         Console.ReadLine();
      }
   }
}

If you observe the above example, we created multiple threads (t1 & t2) and trying to execute the PrintInfo method. Here, in the PrintInfo method, we didn’t use any lock statement so the threads will execute the PrintInfo method simultaneously.

 

When we execute the above program, we will get the result as shown below.

 

i value: 1
i value: 1
i value: 2
i value: 2
i value: 3
i value: 3
i value: 4
i value: 4

If you observe the result, multiple threads started executing the PrintInfo method simultaneously.

 

This is how we can use lock statement in c# to acquire mutual-exclusion of lock for the specified block of code to make sure that at a time, only one thread can execute it based on our requirements.