Skip to content

Commit cb255cd

Browse files
authored
Add a code snippet for System.Threading.Lock (#9943)
1 parent 3eab36b commit cb255cd

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<!-- CA2252: Opt in to preview features before using them (Lock) -->
6+
<EnablePreviewFeatures>true</EnablePreviewFeatures>
7+
</PropertyGroup>
8+
</Project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Threading;
3+
4+
static class Program
5+
{
6+
private static void Main()
7+
{
8+
var dataStructure = new ExampleDataStructure();
9+
dataStructure.Modify();
10+
}
11+
}
12+
13+
// <Snippet1>
14+
public sealed class ExampleDataStructure
15+
{
16+
private readonly Lock _lockObj = new();
17+
18+
public void Modify()
19+
{
20+
lock (_lockObj)
21+
{
22+
// Critical section associated with _lockObj
23+
}
24+
25+
using (_lockObj.EnterScope())
26+
{
27+
// Critical section associated with _lockObj
28+
}
29+
30+
_lockObj.Enter();
31+
try
32+
{
33+
// Critical section associated with _lockObj
34+
}
35+
finally { _lockObj.Exit(); }
36+
37+
if (_lockObj.TryEnter())
38+
{
39+
try
40+
{
41+
// Critical section associated with _lockObj
42+
}
43+
finally { _lockObj.Exit(); }
44+
}
45+
}
46+
}
47+
// </Snippet1>

xml/System.Threading/Lock.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ When using the <xref:System.Threading.Lock.Enter%2A> or <xref:System.Threading.L
2626
- Ensure that the thread exits the lock with <xref:System.Threading.Lock.Exit%2A> even in case of exceptions, such as in C# by using a `try/finally` block.
2727
- When the lock is being entered and exited in a C# `async` method, ensure that there is no `await` between the enter and exit. Locks are held by threads and the code following an `await` might run on a different thread.
2828
29-
It is recommended to use the <xref:System.Threading.Lock.EnterScope%2A> method with a language construct that automatically disposes the returned <xref:System.Threading.Lock.Scope> such as the C# `using` keyword, or to use the C# `lock` keyword, as these ensure that the lock is exited in exceptional cases. These patterns might also have performance benefits over using `Enter/TryEnter` and `Exit`.
29+
It is recommended to use the <xref:System.Threading.Lock.EnterScope%2A> method with a language construct that automatically disposes the returned <xref:System.Threading.Lock.Scope> such as the C# `using` keyword, or to use the C# `lock` keyword, as these ensure that the lock is exited in exceptional cases. These patterns might also have performance benefits over using `Enter/TryEnter` and `Exit`. The following code fragment illustrates various patterns for entering and exiting a lock.
30+
31+
:::code language="csharp" source="~/snippets/csharp/System.Threading/Lock/Overview/UsagePatterns.cs" id="Snippet1":::
3032
3133
When using the C# `lock` keyword or similar to enter and exit a lock, the type of the expression must be precisely `System.Threading.Lock`. If the type of the expression is anything else, such as `Object` or a generic type like `T`, a different implementation that is not interchangeable can be used instead (such as <xref:System.Threading.Monitor>). For more information, see the relevant [compiler speclet](https://github.com/dotnet/csharplang/blob/main/proposals/lock-object.md).
3234

0 commit comments

Comments
 (0)