我们都知道下面的代码是用来形成临界区的。
public class CommonResource
{
public object obj = new object();
public void PopularFunction()
{
lock (obj)
{
///Access variable that we want to protect form being accessed concurrently
///This forms critical section
///My question is what is role'obj' plays in forming critical section.
///How it works behind the scene.
}
///Above code can be written as
Monitor.Enter(obj);
///Access variable that we want to protect form being accessed concurrently
///This forms critical section
///My question is what is role'obj' plays in forming critical section.
///How it works behind the scene.
Monitor.Exit(obj);
}
}
我的问题是 Monitor.Enter 如何在“obj”的帮助下形成一个关键部分。如果我们需要始终传递一个对象,为什么框架不能显式传递任何对象。这背后肯定有什么原因。谁能解释一下?
谢谢,赫曼特