9/3/2023 0 Comments Locks monitor and semaphor![]() Static Mutex m1 = new Mutex(true, "Questpond") Ĭonsole.WriteLine("New Instance created.") Ĭonsole.WriteLine("Instance already acquired.") Let's see a simple example how to ensure thread safety from external threads. In a multiple instance of an application external threads are created so to ensure thread safety from an external threads we need to apply mutex. Mutex provides safety againts the external threads. Mutex ensures the thread safety which are out process that is threads which coming from outside of an application (External threads). Locks/Monitors provides safety againts the threads generated by an application. Locks/Monitors ensures the thread safety which are in process that is threads which are generated by an application (Internal threads) it does not have any control over the threads which are coming from outside of an application. So as you see friends LOCK and MONITOR ensures thread safety and prevent code from showing undesirable results. Its pretty simple whatever code you want to protect at starting point say "Monitor.Enter" and at ending point say "Monitor.Exit" as shown in below code. But here we will also show you same code example using Monitor class. So usage of monitor class or lock is one and same. Lock keyword is just a shot-form of Monitor. Static readonly object _object = new object() To apply lock we need to use "LOCK" keyword as shown in below snippet of code. To resolve this we can use any thread synchronization techniques but here as per situation best approach to use LOCK/Monitor. This is what we were talking about in the thread safety that is abnormal behave of an object in multithreading. Because of that it has created an DividebyZeroException error. So while running this program there could be possibility that while one thread is dividing other thread may be resetting the value to 0. static void main is the Main thread or Parent thread and inside that we have created one more thread object i.e. System.DividebyZeroException this because the method Divide is running in a multi-thread process i.e. When we ran this program we got the following error i.e. Divide method loops 5000 times and takes two random numbers between 1 to 5 and divides themselves. We will take up the same example as we explained in the thread safety section.Ībove code example is a simple console application example which executes Divide function in a multi-threaded way. Here in this example we will first execute a simple console application divide function in a multi-thread environment without thread safety to check the output exception error. In simple words whatever code is enclosed in lock scope only one thread can enter in that lock scope and other threads have to wait till entered thread completes its work/ executes its work. It ensures thread safety and in a multithreading environment it ensures only one thread to be executed in given moment of time. Lock keyword is very important in multithreading. Currently available thread synchronization techniques in c-sharp are Lock/Monitor, Mutex, Semaphore and SemaphoreSlim. The precaution or thread safety can be achieved using proper thread synchronization techniques. ![]() So it means we should take some precaution while working in a multi-threaded environment atleast those lines of code where we feel code can behave abnormally. If these types of errors occurs in a multi-thread environment that means thread is not thread safe. For example : Let's say if we run a divide function with random numbers in a multi-threaded environment since it is running in a multi-threaded environment there could be possibility that two threads are executing divide calculation same time using random numbers can create system.dividebyzeroexception error. If an object behaves abnormally in a multi-threaded environment then that thread is not thread safe. For more information threading click here and for thread pooling see here.īefore we talk about mutex, semaphore, monitor and semaphoreslim and their differences let's understand what is thread safety. Before reading this article I strongly suggest you to read our previous c-sharp articles on threading and types of threading and thread pooling. Hello Friends.! In this article document we will understand how to work on multi-thread environment using mutex, semaphore, monitor and semaphoreslim.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |