filmov
tv
Part 94 Difference between Monitor and lock in C#
Показать описание
Text version of the video
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
Slides
All C# Text Articles
All C# Slides
All Dot Net and SQL Server Tutorials
In this video we will discuss, the Difference between Monitor class and lock.
Both Monitor class and lock provides a mechanism that synchronizes access to objects. lock is the shortcut for Monitor.Enter with try and finally. Meaning the following code
static object _lock = new object();
public static void AddOneMillion()
{
for (int i = 1; i [= 1000000; i++)
{
lock (_lock)
{
Total++;
}
}
}
can be rewritten as shown below.
static object _lock = new object();
public static void AddOneMillion()
{
for (int i = 1; i [= 1000000; i++)
{
// Acquires the exclusive lock
Monitor.Enter(_lock);
try
{
Total++;
}
finally
{
// Releases the exclusive lock
Monitor.Exit(_lock);
}
}
}
// In C# 4, it is implement slightly differently
static object _lock = new object();
public static void AddOneMillion()
{
for (int i = 1; i [= 1000000; i++)
{
bool lockTaken = false;
// Acquires the exclusive lock
Monitor.Enter(_lock, ref lockTaken);
try
{
Total++;
}
finally
{
// Releases the exclusive lock
if(lockTaken)
Monitor.Exit(_lock);
}
}
}
So, in short, lock is a shortcut and it's the option for the basic usage. If you need more control to implement advanced multithreading solutions using TryEnter() Wait(), Pulse(), & PulseAll() methods, then the Montior class is your option.
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
Slides
All C# Text Articles
All C# Slides
All Dot Net and SQL Server Tutorials
In this video we will discuss, the Difference between Monitor class and lock.
Both Monitor class and lock provides a mechanism that synchronizes access to objects. lock is the shortcut for Monitor.Enter with try and finally. Meaning the following code
static object _lock = new object();
public static void AddOneMillion()
{
for (int i = 1; i [= 1000000; i++)
{
lock (_lock)
{
Total++;
}
}
}
can be rewritten as shown below.
static object _lock = new object();
public static void AddOneMillion()
{
for (int i = 1; i [= 1000000; i++)
{
// Acquires the exclusive lock
Monitor.Enter(_lock);
try
{
Total++;
}
finally
{
// Releases the exclusive lock
Monitor.Exit(_lock);
}
}
}
// In C# 4, it is implement slightly differently
static object _lock = new object();
public static void AddOneMillion()
{
for (int i = 1; i [= 1000000; i++)
{
bool lockTaken = false;
// Acquires the exclusive lock
Monitor.Enter(_lock, ref lockTaken);
try
{
Total++;
}
finally
{
// Releases the exclusive lock
if(lockTaken)
Monitor.Exit(_lock);
}
}
}
So, in short, lock is a shortcut and it's the option for the basic usage. If you need more control to implement advanced multithreading solutions using TryEnter() Wait(), Pulse(), & PulseAll() methods, then the Montior class is your option.
Комментарии