This article explains what Thread Locking is and how to use it efficiently in multithreading environment.
- Threading Simplified: Part 1 (Threads Inception)
- Threading Simplified: Part 2 (Multithreading Concepts)
- Threading Simplified: Part 3 (Threads type)
- Threading Simplified: Part 4 (Threads Creation)
- Threading Simplified: Part 5 (Thread Pools)
- Threading Simplified: Part 6 (Exception Handling)
- Threading Simplified: Part 7 (Thread Priority)
- Threading Simplified: Part 8 (Synchronization Basics and Thread Blocking)
Thread Locking, lock keyword
I am again here to continue the discussion around Threading. Today, we will discuss about Thread Locking and related concepts.
Let’s start by putting questions to understand the concepts.
Why Thread Locking?
Locking is used to ensure that only one thread can enter at a time in a code block.
Is Locking Thread Safe by default?
Locking is not thread safe by default and one needs to handle it explicitly to avoid thread clash in a multithreaded environment.
Good so far. Let’s understand it bya simple example.
- class TestThreadSafe
- {
- int num1 = 0;
- int num2 = 0;
- Random rnd = new Random();
- public void DoDivide()
- {
- for(inti = 0; i < 1000000; i++)
- {
- num1 = rnd.Next(1, 5);
- num2 = rnd.Next(1, 5);
- Console.WriteLine(num1 / num2);
- num1 = 0;
- num2 = 0;
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.Title = "Thread Locking Demo";
- TestThreadSafe testThreadSafe = new TestThreadSafe();
- Threadt1 = new Thread(testThreadSafe.DoDivide);
- Threadt2 = new Thread(testThreadSafe.DoDivide);
- t1.Start();
- t2.Start();
- }
- }

Hope you realized the problem what’s going wrong here. In case you are still thinking it's not a problem, it’s a one of the concurrency issue that may occur in multithread environment.
If you see the code closely, you can see that at any given instance, thread1 (or t1) may be at LN3 and thread2 (or t2) may be executing LN5. In that case compiler may throw DivideByZeroException.
- num1 = rnd.Next(1, 5); //LN1
- num2 = rnd.Next(1, 5); //LN2
- Console.WriteLine(num1 / num2); //LN3 thread1
- num1 = 0; //LN4
- num2 = 0;//thread2 //LN5 thread5
Well,the solution is to handle the concurrency properly. Here we can use lock keyword to address the problem.
Let’s fix the issue by adding a variable and modifying the Do Divide method a little bit as in the following.
- class TestThreadSafe
- {
- private static objectmyLock = newobject();
- public void DoDivide()
- {
- lock(myLock) //This will ensure that only one thread can enter into code.
- {
- for(inti = 0; i < 1000000; i++)
- {
- num1 = rnd.Next(1, 5);
- num2 = rnd.Next(1, 5);
- Console.WriteLine(num1 / num2);
- num1 = 0;
- num2 = 0;
- }
- }
- }
- }

As you can see that exception (DivideByZeroException) which we got in the previous example is not happening anymore as we have enclosed the problematic code within the lock keyword which ensures that only one thread can enter into it.
Note: I have seen some people use as lock(this) that locks the complete class instance which at times may lead to a deadlock situation. So the recommended practice is to have a private static variable created in a class and lock that variable only.
Hope you have liked the article. Look forward for your comments/suggestions.


Prakash TripathiTOP 100Posted Mar 7, 2016, 7:16 AM EST
Thnx Ankur.
Ankur MistryTOP 100Posted Mar 7, 2016, 7:16 AM EST
Very nice Prakash Tripathi
Prakash TripathiTOP 100Posted Mar 7, 2016, 2:40 AM EST
Thnx Sibeesh.
Sibeesh VenuTOP 100Posted Mar 7, 2016, 2:29 AM EST
Nice Share
Prakash TripathiTOP 100Posted Mar 7, 2016, 1:27 AM EST
Thnx Vignesh.
Vignesh ManiTOP 100Posted Mar 7, 2016, 1:08 AM EST
Good
Prakash TripathiTOP 100Posted Mar 6, 2016, 11:23 PM EST
Thnx Ibrahim.
Mohammed IbrahimPosted Mar 6, 2016, 11:11 PM EST
nice
Prakash TripathiTOP 100Posted Mar 6, 2016, 9:06 PM EST
Welcome Sonu.
Sonu ChaudharyPosted Mar 6, 2016, 8:17 PM EST
thanks
Prakash TripathiTOP 100Posted Mar 6, 2016, 7:54 PM EST
Thnx Asfend.
Asfend YarPosted Mar 6, 2016, 7:47 PM EST
nice
Prakash TripathiTOP 100Posted Mar 6, 2016, 5:57 PM EST
Thnx Humayun.
Humayun Kabir MamunPosted Mar 6, 2016, 5:41 PM EST
Nice...
Prakash TripathiTOP 100Posted Mar 6, 2016, 4:47 PM EST
Thnx Kumaresh
Prakash TripathiTOP 100Posted Mar 6, 2016, 4:47 PM EST
Thnx Karthiga.
Prakash TripathiTOP 100Posted Mar 6, 2016, 4:46 PM EST
Thnx Asfend.
Kumaresh RajalingamPosted Mar 6, 2016, 3:29 PM EST
good one
Kumaresh RajalingamPosted Mar 6, 2016, 3:29 PM EST
Nice explanation
Sr KarthigaPosted Mar 6, 2016, 3:17 PM EST
god one
Sr KarthigaPosted Mar 6, 2016, 3:17 PM EST
Nice explanation
Asfend YarPosted Mar 6, 2016, 9:58 AM EST
nice