| title | ms.date | ms.prod | ms.technology | ms.topic | f1_keywords | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | translation.priority.ht | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
lock Statement (C# Reference) |
2015-07-20 |
.net |
|
article |
|
|
|
656da1a4-707e-4ef6-9c6e-6d13b646af42 |
43 |
BillWagner |
wiwagn |
|
lock Statement (C# Reference)
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.
class Account
{
decimal balance;
private Object thisLock = new Object();
public void Withdraw(decimal amount)
{
lock (thisLock)
{
if (amount > balance)
{
throw new Exception("Insufficient funds");
}
balance -= amount;
}
}
} For more information, see Thread Synchronization.
Remarks
The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter a locked code, it will wait, block, until the object is released.
The section Threading discusses threading.
The lock keyword calls xref:System.Threading.Monitor.Enter%2A at the start of the block and xref:System.Threading.Monitor.Exit%2A at the end of the block. A xref:System.Threading.ThreadInterruptedException is thrown if xref:System.Threading.Thread.Interrupt%2A interrupts a thread that is waiting to enter a lock statement.
In general, avoid locking on a public type, or instances beyond your code's control. The common constructs lock (this), lock (typeof (MyType)), and lock ("myLock") violate this guideline:
-
lock (this)is a problem if the instance can be accessed publicly. -
lock (typeof (MyType))is a problem ifMyTypeis publicly accessible. -
lock("myLock")is a problem because any other code in the process using the same string, will share the same lock.
Best practice is to define a private object to lock on, or a private static object variable to protect data common to all instances.
You can't use the await keyword in the body of a lock statement.
Example
The following sample shows a simple use of threads without locking in C#.
[!code-cscsrefKeywordsFixedLock#5]
Example
The following sample uses threads and lock. As long as the lock statement is present, the statement block is a critical section and balance will never become a negative number.
[!code-cscsrefKeywordsFixedLock#6]
C# Language Specification
[!INCLUDECSharplangspec]
See Also
xref:System.Reflection.MethodImplAttributes
xref:System.Threading.Mutex
C# Reference
C# Programming Guide
Threading
C# Keywords
Statement Keywords
@System.Threading.Monitor
Interlocked Operations
AutoResetEvent
Thread Synchronization