c# (Csharp) and .NET :- Multithreading and thread safe objects.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we will talk about how to make your objects thread safe in multi-threaded environment now before even I go ahead and I talk about different techniques of how to make your object thread safe let's first try to understand this terminology called as thread safe so in order to understand this terminology you can see that I have a simple class here called as the maths class and this maths class takes two numbers here num1 and num2 and you can see that there is a very nice divided function here and this divided function actually is nothing but it's a big for running loop here you know it runs like 100,000 times and what it does is you know it takes a random numbers into these num1 and num2 variable so you can see that you know I am using the.net random function here and it takes you know random numbers between 1 1 to 2 in number 1 and again it takes random number between 1 to 2 in num2 it divides them right and then it initializes you know both these variables num1 and num2 to 0 okay so we have a very very simple divide function here you know which is running like hundred thousand times and it takes some random numbers and divides them and afterwards it goes and initializes you know both num1 and num2 to 0 so that the next for loop iteration can go properly right now this divide method of the maths class I want to call in a multi-threaded environment now let us try to understand you know what kind of problem you can face here so what I will do is in the static void main I'm going to go and create a thread here called as t1 and first let's go ahead and create the object okay so let's go ahead and create the maths object so maths obj one or vision maths is equal to new maths right and in this thread right you know we're going to go and call the divide function so I'm going to say obj maths obj maths now you know you can see that I am NOT able to see the obj maths here why because we have this method of static so we can only call static objects inside static methods so I'm going to just make it static so we will say obj at start divide alright and we'll say start this thread so what will happen is it will actually go and start you know running this divided function here the divide method here which will actually run hundred thousand times and take the random numbers and divide them okay and also what I will do is now this is you know you can think about this is the child thread right this is more of a child thread right now this static void main also you know runs on a separate thread right so I would also like to go and call obj maths dot divide you know from the main thread right so you can see now that obj maths obj maths or divide is called from the child thread so that is one thread which is running on the maths object and in the same way the main thread that is static void main is also calling this divided function so two threads are now calling concurrently the divide function so what kind of problem we can face okay so what I'll do is I'll just go and run this so I'm going to just do a control f5 here let's see what happens so that it goes and you can see that there is a error here now let's see what the error is so I'm going to just go and close this program to just display the error and we can see that the error says that attempted to divide by zero now how did that happen right so let's try to analyze what happened so let us go back again to a program here this happened because our maths object is not thread safe in other words you know if your object is behaving abnormally under multi-threaded environment you know that means you know you have not taken care of you know the multi-threaded environment now seven than that you know let's analyze you know why this error is coming why the error of divide by zero is coming right so as we said previously we have two threads here one is the main thread and the other one is a child thread oh it's very much possible when both of these threads are calling this divided function you know in a concurrent manner it's possible that you know the main thread is probably dividing executing this line of code at the same time the child thread is over here setting this to zero so what happens is you know he set this to zero the main thread comes here and he is trying to divide and you get the exception right so now the next thing is so what is the solution right so solution is that you know in at least these lines of code at one time only one thread should execute in other words you know let us say the main thread is going and executing you know he is going in what he called getting the random numbers then until he comes to this line of code Ted 2 should not be able to enter these lines of code in other words at least for you know these lines of code only one thread can enter at one time you know I am going to say the multi-threading can happen here in the for loop we don't care about it because there are there is no code over there in which can which can cause abnormal problems right but at least for these lines of code we would like that one thread at a time should be executing it not more than that right so how do we ensure that you know only one thread is executing these lines of code and this kind of abnormal behavior you know we can avoid this can be achieved by using proper thread synchronization thread synchronization techniques I'll say so there are three important thread synchronization techniques currently you know in.net one is the lock you can say it's a law monitor whatever name you want to call the second one is mutex and the last one is semaphore so for now you know for the simple problem you know what should we use right so in order to solve this problem you know we can use something called as a lock or the monitor so what I can do is I can say okay I can say your lock this right so this log keyword here signifies that you know whatever statements are enclosed inside this what you call log scope here at the given moment of time only one thread can execute it so now what should happen is if I do a control f5 I should not get that error so you can see that the complete for loop executed and there was no problem again let me just demonstrate this if I go and comment this right and if I get into a control fi you can see the exception is there if I go and put the lock keyword here so if I go and uncomment this and if I now do a control f5 the error is not there so what happens is you know when you put a lock keyword right or then you use the lock syntax only one thread can execute those lines of code and you know you can have thread safety so I hope that you enjoyed this video in this video we saw that you know how we can make our objects thread safe by using the lock keyword now you know when we talk about safety you know there are three important keywords again you know which which are very important to understand the first one is monitor the second one is mutex and the last one is semaphore so the next part of the video will try to understand what exactly is monitor mutex and semaphore thanks so much
Info
Channel: .NET Interview Preparation videos
Views: 190,996
Rating: undefined out of 5
Keywords: c#, .NET, Multithreading, Thread safety, csharp and .NET, multithreaded programming, what is thread in c#, difference between thread and process in c#, c# thread start method, Multithreading in C# .Net, C# .NET Tutorial, locks in .net, monitor in .net, mutex in .net, multithreading in c#, csharp multithreading, c# and .net for beginners, questpond c# interview questions, questpond c#, safe objects in c#, threading vs multithreading in c#, c# threading tutorial, learn csharp
Id: J-jNcUhi9xw
Channel Id: undefined
Length: 7min 53sec (473 seconds)
Published: Sat Apr 21 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.