diff --git a/snippets/csharp/System/ThreadStaticAttribute/Overview/Project.csproj b/snippets/csharp/System/ThreadStaticAttribute/Overview/Project.csproj
index aa9fd2ecaaf..d260053fe00 100644
--- a/snippets/csharp/System/ThreadStaticAttribute/Overview/Project.csproj
+++ b/snippets/csharp/System/ThreadStaticAttribute/Overview/Project.csproj
@@ -3,6 +3,7 @@
Exe
net6.0
+ enable
-
+
\ No newline at end of file
diff --git a/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs b/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs
index 9e42b59c289..fcc8fd8c3d2 100644
--- a/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs
+++ b/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs
@@ -2,108 +2,43 @@
using System;
using System.Threading;
-public class Example
+class Program
{
- [ThreadStatic] static double previous = 0.0;
- [ThreadStatic] static double sum = 0.0;
- [ThreadStatic] static int calls = 0;
- [ThreadStatic] static bool abnormal;
- static int totalNumbers = 0;
- static CountdownEvent countdown;
- private static Object lockObj;
- Random rand;
-
- public Example()
- {
- rand = new Random();
- lockObj = new Object();
- countdown = new CountdownEvent(1);
- }
+ [ThreadStatic]
+ private static string? _requestId;
- public static void Main()
- {
- Example ex = new Example();
- Thread.CurrentThread.Name = "Main";
- ex.Execute();
- countdown.Wait();
- Console.WriteLine("{0:N0} random numbers were generated.", totalNumbers);
- }
+ static void Main()
+ {
+ Thread thread1 = new(ProcessRequest);
+ Thread thread2 = new(ProcessRequest);
- private void Execute()
- {
- for (int threads = 1; threads <= 10; threads++)
- {
- Thread newThread = new Thread(new ThreadStart(this.GetRandomNumbers));
- countdown.AddCount();
- newThread.Name = threads.ToString();
- newThread.Start();
- }
- this.GetRandomNumbers();
- }
+ thread1.Start("REQ-001");
+ thread2.Start("REQ-002");
- private void GetRandomNumbers()
- {
- double result = 0.0;
+ thread1.Join();
+ thread2.Join();
- for (int ctr = 0; ctr < 2000000; ctr++)
- {
- lock (lockObj) {
- result = rand.NextDouble();
- calls++;
- Interlocked.Increment(ref totalNumbers);
- // We should never get the same random number twice.
- if (result == previous) {
- abnormal = true;
- break;
- }
- else {
- previous = result;
- sum += result;
- }
- }
- }
- // get last result
- if (abnormal)
- Console.WriteLine("Result is {0} in {1}", previous, Thread.CurrentThread.Name);
-
- Console.WriteLine("Thread {0} finished random number generation.", Thread.CurrentThread.Name);
- Console.WriteLine("Sum = {0:N4}, Mean = {1:N4}, n = {2:N0}\n", sum, sum/calls, calls);
- countdown.Signal();
- }
+ Console.WriteLine("Main thread execution completed.");
+ }
+
+ static void ProcessRequest(object? requestId)
+ {
+ // Assign the request ID to the thread-static field
+ _requestId = requestId as string;
+
+ // Simulate request processing across multiple method calls
+ PerformDatabaseOperation();
+ PerformLogging();
+ }
+
+ static void PerformDatabaseOperation()
+ {
+ Console.WriteLine($"Thread {Environment.CurrentManagedThreadId}: Processing DB operation for request {_requestId}");
+ }
+
+ static void PerformLogging()
+ {
+ Console.WriteLine($"Thread {Environment.CurrentManagedThreadId}: Logging request {_requestId}");
+ }
}
-// The example displays output similar to the following:
-// Thread 1 finished random number generation.
-// Sum = 1,000,556.7483, Mean = 0.5003, n = 2,000,000
-//
-// Thread 6 finished random number generation.
-// Sum = 999,704.3865, Mean = 0.4999, n = 2,000,000
-//
-// Thread 2 finished random number generation.
-// Sum = 999,680.8904, Mean = 0.4998, n = 2,000,000
-//
-// Thread 10 finished random number generation.
-// Sum = 999,437.5132, Mean = 0.4997, n = 2,000,000
-//
-// Thread 8 finished random number generation.
-// Sum = 1,000,663.7789, Mean = 0.5003, n = 2,000,000
-//
-// Thread 4 finished random number generation.
-// Sum = 999,379.5978, Mean = 0.4997, n = 2,000,000
-//
-// Thread 5 finished random number generation.
-// Sum = 1,000,011.0605, Mean = 0.5000, n = 2,000,000
-//
-// Thread 9 finished random number generation.
-// Sum = 1,000,637.4556, Mean = 0.5003, n = 2,000,000
-//
-// Thread Main finished random number generation.
-// Sum = 1,000,676.2381, Mean = 0.5003, n = 2,000,000
-//
-// Thread 3 finished random number generation.
-// Sum = 999,951.1025, Mean = 0.5000, n = 2,000,000
-//
-// Thread 7 finished random number generation.
-// Sum = 1,000,844.5217, Mean = 0.5004, n = 2,000,000
-//
-// 22,000,000 random numbers were generated.
//
diff --git a/snippets/fsharp/System/ThreadStaticAttribute/Overview/threadsafe2a.fs b/snippets/fsharp/System/ThreadStaticAttribute/Overview/threadsafe2a.fs
index d2d1aec32ef..6e76d8a93f1 100644
--- a/snippets/fsharp/System/ThreadStaticAttribute/Overview/threadsafe2a.fs
+++ b/snippets/fsharp/System/ThreadStaticAttribute/Overview/threadsafe2a.fs
@@ -2,95 +2,31 @@
open System
open System.Threading
-type Example() =
- []
- static val mutable private previous : double
+type ThreadLocal() =
+ []
+ static val mutable private requestId: string option
- []
- static val mutable private sum : double
-
- []
- static val mutable private calls : int
+ static member PerformLogging() =
+ Console.WriteLine($"Thread {Environment.CurrentManagedThreadId}: Logging request {ThreadLocal.requestId.Value}")
- []
- static val mutable private abnormal : bool
-
- static let mutable totalNumbers = 0
- static let countdown = new CountdownEvent(1)
- static let lockObj = obj ()
- let rand = Random()
+ static member PerformDatabaseOperation() =
+ Console.WriteLine($"Thread {Environment.CurrentManagedThreadId}: Processing DB operation for request {ThreadLocal.requestId.Value}")
+ static member ProcessRequest(reqId: obj) =
+ ThreadLocal.requestId <- Some(reqId :?> string)
+ ThreadLocal.PerformDatabaseOperation()
+ ThreadLocal.PerformLogging()
- member this.Execute() =
- for threads = 1 to 10 do
- let newThread = new Thread(ThreadStart this.GetRandomNumbers)
- countdown.AddCount()
- newThread.Name <- threads.ToString()
- newThread.Start()
- this.GetRandomNumbers()
- countdown.Wait()
- printfn $"{totalNumbers:N0} random numbers were generated."
+[]
+let main _ =
+ let thread1 = Thread(ThreadStart(fun () -> ThreadLocal.ProcessRequest("REQ-001")))
+ let thread2 = Thread(ThreadStart(fun () -> ThreadLocal.ProcessRequest("REQ-002")))
- member _.GetRandomNumbers() =
- let mutable i = 0
- while i < 2000000 do
- lock lockObj (fun () ->
- let result = rand.NextDouble()
- Example.calls <- Example.calls + 1
- Interlocked.Increment &totalNumbers |> ignore
- // We should never get the same random number twice.
- if result = Example.previous then
- Example.abnormal <- true
- i <- 2000001 // break
- else
- Example.previous <- result
- Example.sum <- Example.sum + result )
- i <- i + 1
- // get last result
- if Example.abnormal then
- printfn $"Result is {Example.previous} in {Thread.CurrentThread.Name}"
-
- printfn $"Thread {Thread.CurrentThread.Name} finished random number generation."
- printfn $"Sum = {Example.sum:N4}, Mean = {Example.sum / float Example.calls:N4}, n = {Example.calls:N0}\n"
- countdown.Signal() |> ignore
+ thread1.Start()
+ thread2.Start()
+ thread1.Join()
+ thread2.Join()
-let ex = Example()
-Thread.CurrentThread.Name <- "Main"
-ex.Execute()
-
-// The example displays output similar to the following:
-// Thread 1 finished random number generation.
-// Sum = 1,000,556.7483, Mean = 0.5003, n = 2,000,000
-//
-// Thread 6 finished random number generation.
-// Sum = 999,704.3865, Mean = 0.4999, n = 2,000,000
-//
-// Thread 2 finished random number generation.
-// Sum = 999,680.8904, Mean = 0.4998, n = 2,000,000
-//
-// Thread 10 finished random number generation.
-// Sum = 999,437.5132, Mean = 0.4997, n = 2,000,000
-//
-// Thread 8 finished random number generation.
-// Sum = 1,000,663.7789, Mean = 0.5003, n = 2,000,000
-//
-// Thread 4 finished random number generation.
-// Sum = 999,379.5978, Mean = 0.4997, n = 2,000,000
-//
-// Thread 5 finished random number generation.
-// Sum = 1,000,011.0605, Mean = 0.5000, n = 2,000,000
-//
-// Thread 9 finished random number generation.
-// Sum = 1,000,637.4556, Mean = 0.5003, n = 2,000,000
-//
-// Thread Main finished random number generation.
-// Sum = 1,000,676.2381, Mean = 0.5003, n = 2,000,000
-//
-// Thread 3 finished random number generation.
-// Sum = 999,951.1025, Mean = 0.5000, n = 2,000,000
-//
-// Thread 7 finished random number generation.
-// Sum = 1,000,844.5217, Mean = 0.5004, n = 2,000,000
-//
-// 22,000,000 random numbers were generated.
+ Console.WriteLine("Main thread execution completed.")
+ 0
//
\ No newline at end of file
diff --git a/snippets/visualbasic/VS_Snippets_CLR_System/system.threadstaticattribute/vb/Program.vbproj b/snippets/visualbasic/VS_Snippets_CLR_System/system.threadstaticattribute/vb/Program.vbproj
new file mode 100644
index 00000000000..adbde6e0619
--- /dev/null
+++ b/snippets/visualbasic/VS_Snippets_CLR_System/system.threadstaticattribute/vb/Program.vbproj
@@ -0,0 +1,8 @@
+
+
+
+ Exe
+ net6.0
+
+
+
\ No newline at end of file
diff --git a/snippets/visualbasic/VS_Snippets_CLR_System/system.threadstaticattribute/vb/threadsafe2a.vb b/snippets/visualbasic/VS_Snippets_CLR_System/system.threadstaticattribute/vb/threadsafe2a.vb
index 0b8727643b7..0f9fb059358 100644
--- a/snippets/visualbasic/VS_Snippets_CLR_System/system.threadstaticattribute/vb/threadsafe2a.vb
+++ b/snippets/visualbasic/VS_Snippets_CLR_System/system.threadstaticattribute/vb/threadsafe2a.vb
@@ -2,105 +2,43 @@
Option Strict On
'
+Imports System
Imports System.Threading
-Public Class Example
- Shared previous As Double = 0.0
- Shared sum As Double = 0.0
- Shared calls As Integer = 0
- Shared abnormal As Boolean
- Shared totalNumbers As Integer = 0
- Shared countdown As CountdownEvent
- Private Shared lockObj As Object
- Dim rand As Random
+Module Program
- Public Sub New()
- rand = New Random()
- lockObj = New Object()
- countdown = New CountdownEvent(1)
- End Sub
+
+ Private _requestId As String
- Public Shared Sub Main()
- Dim ex As New Example()
- Thread.CurrentThread.Name = "Main"
- ex.Execute()
- countdown.Wait()
- Console.WriteLine("{0:N0} random numbers were generated.", totalNumbers)
- End Sub
+ Sub Main()
+ Dim thread1 As New Thread(AddressOf ProcessRequest)
+ Dim thread2 As New Thread(AddressOf ProcessRequest)
- Private Sub Execute()
- For threads As Integer = 1 To 10
- Dim newThread As New Thread(New ThreadStart(AddressOf GetRandomNumbers))
- countdown.AddCount()
- newThread.Name = threads.ToString()
- newThread.Start()
- Next
- Me.GetRandomNumbers()
- End Sub
+ thread1.Start("REQ-001")
+ thread2.Start("REQ-002")
- Private Sub GetRandomNumbers()
- Dim result As Double = 0.0
-
-
- For ctr As Integer = 1 To 2000000
- SyncLock lockObj
- result = rand.NextDouble()
- calls += 1
- Interlocked.Increment(totalNumbers)
- ' We should never get the same random number twice.
- If result = previous Then
- abnormal = True
- Exit For
- Else
- previous = result
- sum += result
- End If
- End SyncLock
- Next
- ' Get last result.
- If abnormal Then
- Console.WriteLine("Result is {0} in {1}", previous, Thread.CurrentThread.Name)
- End If
-
- Console.WriteLine("Thread {0} finished random number generation.", Thread.CurrentThread.Name)
- Console.WriteLine("Sum = {0:N4}, Mean = {1:N4}, n = {2:N0}", sum, sum/calls, calls)
- Console.WriteLine()
- countdown.Signal()
- End Sub
-End Class
-' The example displays output similar to the following:
-' Thread 1 finished random number generation.
-' Sum = 1,000,556.7483, Mean = 0.5003, n = 2,000,000
-'
-' Thread 6 finished random number generation.
-' Sum = 999,704.3865, Mean = 0.4999, n = 2,000,000
-'
-' Thread 2 finished random number generation.
-' Sum = 999,680.8904, Mean = 0.4998, n = 2,000,000
-'
-' Thread 10 finished random number generation.
-' Sum = 999,437.5132, Mean = 0.4997, n = 2,000,000
-'
-' Thread 8 finished random number generation.
-' Sum = 1,000,663.7789, Mean = 0.5003, n = 2,000,000
-'
-' Thread 4 finished random number generation.
-' Sum = 999,379.5978, Mean = 0.4997, n = 2,000,000
-'
-' Thread 5 finished random number generation.
-' Sum = 1,000,011.0605, Mean = 0.5000, n = 2,000,000
-'
-' Thread 9 finished random number generation.
-' Sum = 1,000,637.4556, Mean = 0.5003, n = 2,000,000
-'
-' Thread Main finished random number generation.
-' Sum = 1,000,676.2381, Mean = 0.5003, n = 2,000,000
-'
-' Thread 3 finished random number generation.
-' Sum = 999,951.1025, Mean = 0.5000, n = 2,000,000
-'
-' Thread 7 finished random number generation.
-' Sum = 1,000,844.5217, Mean = 0.5004, n = 2,000,000
-'
-' 22,000,000 random numbers were generated.
+ thread1.Join()
+ thread2.Join()
+
+ Console.WriteLine("Main thread execution completed.")
+ End Sub
+
+ Sub ProcessRequest(ByVal requestId As Object)
+ ' Assign the request ID to the thread-static field
+ _requestId = requestId.ToString()
+
+ ' Simulate request processing across multiple method calls
+ PerformDatabaseOperation()
+ PerformLogging()
+ End Sub
+
+ Sub PerformDatabaseOperation()
+ Console.WriteLine($"Thread {Environment.CurrentManagedThreadId}: Processing DB operation for request {_requestId}")
+ End Sub
+
+ Sub PerformLogging()
+ Console.WriteLine($"Thread {Environment.CurrentManagedThreadId}: Logging request {_requestId}")
+ End Sub
+
+End Module
'