Introduction to Lock in C#
The construct used to ensure that no other thread can enter the section of code in which a thread is already executing is called lock construct in C#. The other thread attempting to enter the section of code in which a thread is already executing is made to wait of blocked until the execution of the thread which is already executing in the section of code is complete and making use of lock is a faster and convenient way to handle the threads in multithreading programming. This lock is released after the completion of execution of the thread which is already executing in the section of code allowing the other threads to execute in the section of code.
Syntax
lock(object_name) statement_block
Where,
- object_name is the name of the object on which the lock must be put on.
- Statement_block specifies the block of code that must be executed once the lock is acquired on a given thread.
How does Lock work in C#?
- Whenever there is a need for the thread to execute in a section of code without the interruption of any other thread, we make use of lock to make sure only one thread can access that section of code at a time.
- The other threads trying to access the section of code that is locked by making of lock is made to wait or blocked until the execution of the thread that is already executing in the section of code after acquiring the lock is complete.
- Making use of lock is a more faster and convenient way to handle the threads in multithreading programming.
- The lock is released after the completion of execution of the thread which is already executing in the section of code allowing the other threads to execute in the section of code.
Examples to Implement Lock in C#
Below are the examples mentioned :
Example #1
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, a namespace called program is defined. Then a class called check is defined. Then an object that defined a lock is created. Then 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 the thread that is already executing completes its execution. Then the keyword lock is used to lock the object that was created earlier. Then the output is displayed synchronously in a row by one thread followed by another thread because we have used lock on display method. The output is shown in the snapshot above.
Example #2
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();
}
}
}
4.6 (7,963 ratings)
View Course
Output:
Explanation: In the above program, a namespace called program is defined. Then a class called check is defined. Then an object that defined a lock is created. Then 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 the thread that is already executing completes its execution. Then the keyword lock is used to lock the object that was created earlier. Then the output is displayed synchronously in a row by one thread followed by another thread because we have used lock on display method. The output is shown in the snapshot above.
Conclusion
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.
Recommended Articles
This is a guide to Lock in C#. Here we discuss an introduction to Lock in C#, syntax, how does it work with examples to understand better. You can also go through our other related articles to learn more –