Home > Article > Backend Development > Lock in C#
The lock construct in C# ensures that no other thread can enter the section of code where a thread is already executing. It makes the other thread attempting to enter the code section wait or block until the executing thread completes its execution. Using a lock is a faster and more convenient way to handle threads in multithreading programming.
Syntax
lock(object_name) statement_block
Where,
Find the examples below.
C# program to demonstrate the lock to block the execution of another thread while a thread is already executing in the critical section of the code:
Code:
using System; using System.Threading; //a namespace called program is defined namespace program { //a class called check is defined class check { //an object that defines a lock is created static readonly object lockname = new object(); //a method called display is created in which the lock is used to make any other threads trying to access the method wait or block until thread that is already executing completes its execution static void display() { //keyword lock is used to lock the object lock (lockname) { for (int a = 1; a <= 3; a++) { //the output is displayed synchronously in a row by one thread followed by another thread because we have used lock on display method Console.WriteLine("The value to be printed is: {0}", a); } } } static void Main(string[] args) { //an instance of the thread is created and the corresponding thread is executed on the display method Thread firstthread = new Thread(display); //an instance of the thread is created and the corresponding thread is executed on the display method Thread secondthread = new Thread(display); firstthread.Start(); secondthread.Start(); Console.ReadLine(); } } }
Output:
Explanation: In the above program, the program defines a namespace called “program”. It then defines a class named “check.” This method utilizes the lock to make any other threads attempting to access it wait or block until the currently executing thread finishes its execution. The snapshot above displays the output.
C# program to demonstrate the lock to block the execution of another thread while a thread is already executing in the critical section of the code:
Code:
using System; using System.Threading; //a namespace called program is defined namespace program { //a class called check is defined class check { //an object that defines a lock is created static readonly object lockname = new object(); //a method called display is created in which the lock is used to make any other threads trying to access the method wait or block until thread that is already executing completes its execution static void display() { //keyword lock is used to lock the object lock (lockname) { for (int a = 1; a <= 3; a++) { //the output is displayed synchronously in a row by one thread followed by another thread because we have used lock on display method Console.WriteLine("The first three lines are printed by first thread and the second three lines are printed by the second thread"); } } } static void Main(string[] args) { //an instance of the thread is created and the corresponding thread is executed on the display method Thread firstthread = new Thread(display); //an instance of the thread is created and the corresponding thread is executed on the display method Thread secondthread = new Thread(display); firstthread.Start(); secondthread.Start(); Console.ReadLine(); } } }
Output:
Explanation: The program defines a namespace called “program” and then defines a class called “check.” It creates an object that represents a lock. It uses the keyword “lock” to lock the previously created object.
In this tutorial, we understand the concept of lock-in C# through definition, syntax, and working of the lock through programming examples and their outputs.
The above is the detailed content of Lock in C#. For more information, please follow other related articles on the PHP Chinese website!