From 8aa1894ab523063e7ef7ec0dc42cf1ab1a0c638e Mon Sep 17 00:00:00 2001
From: Koundinya Veluri <kouvel@microsoft.com>
Date: Tue, 21 May 2024 19:44:41 -0700
Subject: [PATCH 1/2] Add code snippet for System.Threading.Lock

This reverts commit 6d46e61a411c85e6b5a643d293bbc0e86997d5d0.
---
 .../Lock/Overview/Project.csproj              |  8 ++++
 .../Lock/Overview/UsagePatterns.cs            | 38 +++++++++++++++++++
 xml/System.Threading/Lock.xml                 |  4 +-
 3 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100644 snippets/csharp/System.Threading/Lock/Overview/Project.csproj
 create mode 100644 snippets/csharp/System.Threading/Lock/Overview/UsagePatterns.cs

diff --git a/snippets/csharp/System.Threading/Lock/Overview/Project.csproj b/snippets/csharp/System.Threading/Lock/Overview/Project.csproj
new file mode 100644
index 00000000000..0ea05c389bc
--- /dev/null
+++ b/snippets/csharp/System.Threading/Lock/Overview/Project.csproj
@@ -0,0 +1,8 @@
+<Project Sdk="Microsoft.NET.Sdk">
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>net9.0</TargetFramework>
+    <!-- CA2252: Opt in to preview features before using them (Lock) -->
+    <EnablePreviewFeatures>true</EnablePreviewFeatures>
+  </PropertyGroup>
+</Project>
diff --git a/snippets/csharp/System.Threading/Lock/Overview/UsagePatterns.cs b/snippets/csharp/System.Threading/Lock/Overview/UsagePatterns.cs
new file mode 100644
index 00000000000..41da189fc35
--- /dev/null
+++ b/snippets/csharp/System.Threading/Lock/Overview/UsagePatterns.cs
@@ -0,0 +1,38 @@
+﻿using System;
+using System.Threading;
+
+// <Snippet1>
+public sealed class ExampleDataStructure
+{
+    private readonly Lock _lockObj = new();
+
+    private void Modify()
+    {
+        lock (_lockObj)
+        {
+            // Critical section associated with _lockObj
+        }
+
+        using (_lockObj.EnterScope())
+        {
+            // Critical section associated with _lockObj
+        }
+
+        _lockObj.Enter();
+        try
+        {
+            // Critical section associated with _lockObj
+        }
+        finally { _lockObj.Exit(); }
+
+        if (_lockObj.TryEnter())
+        {
+            try
+            {
+                // Critical section associated with _lockObj
+            }
+            finally { _lockObj.Exit(); }
+        }
+    }
+}
+// </Snippet1>
diff --git a/xml/System.Threading/Lock.xml b/xml/System.Threading/Lock.xml
index 0594396c4db..a0e5d0abe48 100644
--- a/xml/System.Threading/Lock.xml
+++ b/xml/System.Threading/Lock.xml
@@ -26,7 +26,9 @@ When using the <xref:System.Threading.Lock.Enter%2A> or <xref:System.Threading.L
 - 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.
 - 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.
 
-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`.
+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.
+
+:::code language="csharp" source="~/snippets/csharp/System.Threading/Lock/Overview/UsagePatterns.cs" id="Snippet1":::
 
 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).
 

From 5468299eeaac16076835854451c2838b745a4c3c Mon Sep 17 00:00:00 2001
From: Koundinya Veluri <kouvel@microsoft.com>
Date: Wed, 22 May 2024 08:39:18 -0700
Subject: [PATCH 2/2] Make code snippet buildable

---
 .../System.Threading/Lock/Overview/UsagePatterns.cs   | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/snippets/csharp/System.Threading/Lock/Overview/UsagePatterns.cs b/snippets/csharp/System.Threading/Lock/Overview/UsagePatterns.cs
index 41da189fc35..14294e99d8e 100644
--- a/snippets/csharp/System.Threading/Lock/Overview/UsagePatterns.cs
+++ b/snippets/csharp/System.Threading/Lock/Overview/UsagePatterns.cs
@@ -1,12 +1,21 @@
 ﻿using System;
 using System.Threading;
 
+static class Program
+{
+    private static void Main()
+    {
+        var dataStructure = new ExampleDataStructure();
+        dataStructure.Modify();
+    }
+}
+
 // <Snippet1>
 public sealed class ExampleDataStructure
 {
     private readonly Lock _lockObj = new();
 
-    private void Modify()
+    public void Modify()
     {
         lock (_lockObj)
         {
