lock Statement (C# Reference)

项目中有遇到lock,由于菜的不能再菜的我木有接触过这个,特地查了msdn。翻译学习如下:

The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lock. The following example includes a lock statement.

lock 关键字将语句块标记为临界区,方法是获取给定对象的互斥锁,执行语句,然后释放该锁。

Object thisLock = new Object();

lock(thisLock)

{

      //Critical code section.

}

lock关键字可确保当一个线程位于代码的临界区时,另一个线程不会进入该临界区。如果其他线程尝试进入锁定的代码,则它将一直等待(即被阻止),直到该对象被释放。

lock 关键字在块的开始处调用Enter,而在块的结尾处调用Exit。

通常,应避免锁定public类型,否则实例将超出代码的控制范围。常见的结构lock(this)、lock(typeof(MyType))和lock(“mylock”)违反此准则:

  • 如果实例可以被公共访问,将出现lock(this)问题。
  • 如果MyType可以被公共访问,将出现lock(typeof(MyType))问题。
  • 由于进程中使用同一字符串的任何其他代码都将共享同一个锁,所以出现lock("mylock")问题。

最佳做法是定义private 对象来锁定,或者private static对象变量来保护所有实例所共有的数据。

下面演示在C#中使用未锁定的线程的简单示例。

//using System.Threading;

class ThreadTest
{
    public void RunMe()
    {
        Console.WriteLine("RunMe callled");
    }

    static void Main()
    {
        ThreadTest b = new ThreadTest();
        Thread t = new Thread(b,RunMe);
         t.Start();
    }
}
//Output : RunMe called

下例使用线程和lock。只要lock语句存在,语句块就是临界区并且balance永远不会是负数。

// using System.Threading;

    class Account
    {
        private Object thisLock = new Object();
        int balance;

        Random r = new Random();

        public Account(int initial)
        {
            balance = initial;
        }

        int Withdraw(int amount)
        {

            // This condition never is true unless the lock statement
            // is commented out.
            if (balance < 0)
            {
                throw new Exception("Negative Balance");
            }

            // Comment out the next line to see the effect of leaving out 
            // the lock keyword.
            lock (thisLock)
            {
                if (balance >= amount)
                {
                    Console.WriteLine("Balance before Withdrawal :  " + balance);
                    Console.WriteLine("Amount to Withdraw        : -" + amount);
                    balance = balance - amount;
                    Console.WriteLine("Balance after Withdrawal  :  " + balance);
                    return amount;
                }
                else
                {
                    return 0; // transaction rejected
                }
            }
        }

        public void DoTransactions()
        {
            for (int i = 0; i < 100; i++)
            {
                Withdraw(r.Next(1, 100));
            }
        }
    }

    class Test
    {
        static void Main()
        {
            Thread[] threads = new Thread[10];
            Account acc = new Account(1000);
            for (int i = 0; i < 10; i++)
            {
                Thread t = new Thread(new ThreadStart(acc.DoTransactions));
                threads[i] = t;
            }
            for (int i = 0; i < 10; i++)
            {
                threads[i].Start();
            }
        }
    }

原文地址:https://www.cnblogs.com/Monica-xu/p/4022727.html