C # lock keyword / lock statement block, thread-locking

A, lock keyword description

1. lock  key block of statements is marked as critical section, the object is to obtain a given mutex statement is executed, and then releases the lock.

  2. lock statement block lock function equivalent to

[csharp]  view plain  copy
  1. Monitor.Enter(obj);   
  2. // snippet  
  3. Monitor.Exit(obj);  

  3. lock to lock a block of statements and Monitor thread locks can not be cross-process synchronization

Second, Remarks

lock Keywords can ensure that when a thread is located in a critical area code when another thread does not enter the critical region. If another thread attempts to enter the lock code, it will have to wait (ie stop) until the object is released.

Thread  This section discusses the thread.

lock Keyword call at the beginning of the block  the Enter , while calling at the end of the block  ExitThreadInterruptedException  triggered, if  Interrupt  interrupt latency input  lock thread statement.

Typically, to avoid locking  public type, instance or beyond the control of the code. Common structure  lock (this), lock (typeof (MyType)) and  lock ("myLock") violation of this guideline:

  • If the instance can be public access, there will be  lock (this) problems.

  • If you  MyType can be public access, there will be  lock (typeof (MyType)) problems.

  • Since the process used in any other code the same string will share the same lock, so there are  lock("myLock") problems.

Best practice is to define  private the object to lock, or  private static object variable to protect data common to all instances.

In  lock the text of the statement can not be used  to wait for  the keyword.

Third, special instructions

1.lock statement must be locked object reference type, not a value type

2. To avoid deadlocks, lock the target object needs to be private

3. In order to avoid the uniqueness lock object, the lock object through private static or private readonly static

Fourth, use the sample

[csharp]  view plain  copy
  1. private readonly static object _MyLock = new object();  
  2. // lock is recommended for static private static variable  
  3. //private readonly object _MyLock = new object();  
  4. /// <summary>  
  5. /// transactions, multi-table modification  
  6. /// </summary>  
  7. /// <param name="name"></param>  
  8. /// <returns></returns>  
  9. public bool UpdateName(string name)  
  10. {  
  11.     lock (_MyLock)  
  12.     {  
  13.         using (var tran = new TransactionScope())  
  14.         {  
  15.             ModuleOperate _module = new ModuleOperate();  
  16.             // 1. Modify module name  
  17.             _module.UpdateFirstName("模块:" + name);  
  18.             // 2. Modify menu  
  19.             this.UpdateFirstName("菜单:" + name);  
  20.             // commit the transaction  
  21.             tran.Complete();  
  22.         }  
  23.     }  
  24.     return true;  
  25. }  

Guess you like

Origin blog.csdn.net/xiong451823652/article/details/78107732