From 30943f6d2a50e4c72bafa3641128d79d949cf1ea Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Fri, 14 Mar 2025 12:09:29 -0700 Subject: [PATCH 1/6] Update ThreadStatic example --- .../Overview/threadsafe2a.cs | 48 +++++++++++-------- .../Overview/threadsafe2a.fs | 42 +++++++++------- .../vb/threadsafe2a.vb | 45 +++++++++-------- 3 files changed, 76 insertions(+), 59 deletions(-) diff --git a/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs b/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs index 9e42b59c289..974d58af37f 100644 --- a/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs +++ b/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs @@ -4,21 +4,21 @@ public class Example { - [ThreadStatic] static double previous = 0.0; - [ThreadStatic] static double sum = 0.0; - [ThreadStatic] static int calls = 0; + [ThreadStatic] static double previous; + [ThreadStatic] static double sum; + [ThreadStatic] static int calls; [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); - } + } public static void Main() { @@ -30,7 +30,7 @@ public static void Main() } private void Execute() - { + { for (int threads = 1; threads <= 10; threads++) { Thread newThread = new Thread(new ThreadStart(this.GetRandomNumbers)); @@ -43,6 +43,12 @@ private void Execute() private void GetRandomNumbers() { + // Initialize ThreadStatic fields for this thread. + previous = 0.0; + sum = 0.0; + calls = 0; + abnormal = false; + double result = 0.0; for (int ctr = 0; ctr < 2000000; ctr++) @@ -59,51 +65,51 @@ private void GetRandomNumbers() 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); + Console.WriteLine("Sum = {0:N4}, Mean = {1:N4}, n = {2:N0}\n", sum, sum/calls, calls); countdown.Signal(); } } // 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..9e3ca150e03 100644 --- a/snippets/fsharp/System/ThreadStaticAttribute/Overview/threadsafe2a.fs +++ b/snippets/fsharp/System/ThreadStaticAttribute/Overview/threadsafe2a.fs @@ -3,18 +3,18 @@ open System open System.Threading type Example() = - [] + [] static val mutable private previous : double - [] + [] static val mutable private sum : double - - [] + + [] static val mutable private calls : int - [] + [] static val mutable private abnormal : bool - + static let mutable totalNumbers = 0 static let countdown = new CountdownEvent(1) static let lockObj = obj () @@ -32,6 +32,12 @@ type Example() = printfn $"{totalNumbers:N0} random numbers were generated." member _.GetRandomNumbers() = + // Initialize ThreadStatic fields for this thread. + Example.previous <- 0.0 + Example.sum <- 0.0 + Example.calls <- 0 + Example.abnormal <- false + let mutable i = 0 while i < 2000000 do lock lockObj (fun () -> @@ -49,7 +55,7 @@ type Example() = // 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 @@ -61,36 +67,36 @@ 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. // \ 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..e8eda6493f4 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 @@ -5,9 +5,9 @@ Option Strict On Imports System.Threading Public Class Example - Shared previous As Double = 0.0 - Shared sum As Double = 0.0 - Shared calls As Integer = 0 + Shared previous As Double + Shared sum As Double + Shared calls As Integer Shared abnormal As Boolean Shared totalNumbers As Integer = 0 Shared countdown As CountdownEvent @@ -39,9 +39,14 @@ Public Class Example End Sub Private Sub GetRandomNumbers() + ' Initialize ThreadStatic fields for this thread. + previous = 0.0 + sum = 0.0 + calls = 0 + abnormal = False + Dim result As Double = 0.0 - - + For ctr As Integer = 1 To 2000000 SyncLock lockObj result = rand.NextDouble() @@ -54,53 +59,53 @@ Public Class Example Else previous = result sum += result - End If + End If End SyncLock Next ' Get last result. If abnormal Then Console.WriteLine("Result is {0} in {1}", previous, Thread.CurrentThread.Name) - End If - + 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() + 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. ' From cc9d1bece0a30e434a0c7dda4dd9c2e31931e817 Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Fri, 14 Mar 2025 13:26:04 -0700 Subject: [PATCH 2/6] Add projection to VB. --- .../system.threadstaticattribute/vb/Program.vbproj | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 snippets/visualbasic/VS_Snippets_CLR_System/system.threadstaticattribute/vb/Program.vbproj 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 From 0afabb53ec9ad0b91024ef0a35b89b74e7938141 Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Fri, 14 Mar 2025 23:03:28 -0700 Subject: [PATCH 3/6] Feedback --- .../Overview/Project.csproj | 3 +- .../Overview/threadsafe2a.cs | 145 +++++------------- .../Overview/threadsafe2a.fs | 110 +++---------- .../vb/threadsafe2a.vb | 127 ++++----------- 4 files changed, 89 insertions(+), 296 deletions(-) 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 974d58af37f..c7ae21e8592 100644 --- a/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs +++ b/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs @@ -2,114 +2,43 @@ using System; using System.Threading; -public class Example +class Program { - [ThreadStatic] static double previous; - [ThreadStatic] static double sum; - [ThreadStatic] static int calls; - [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); - } - - 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); - } - - 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(); - } - - private void GetRandomNumbers() - { - // Initialize ThreadStatic fields for this thread. - previous = 0.0; - sum = 0.0; - calls = 0; - abnormal = false; - - double result = 0.0; - - 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(); - } + [ThreadStatic] + private static string? _requestId; + + static void Main() + { + Thread thread1 = new Thread(ProcessRequest); + Thread thread2 = new Thread(ProcessRequest); + + thread1.Start("REQ-001"); + thread2.Start("REQ-002"); + + thread1.Join(); + thread2.Join(); + + 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 {Thread.CurrentThread.ManagedThreadId}: Processing DB operation for request {_requestId}"); + } + + static void PerformLogging() + { + Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId}: 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 9e3ca150e03..80420537fac 100644 --- a/snippets/fsharp/System/ThreadStaticAttribute/Overview/threadsafe2a.fs +++ b/snippets/fsharp/System/ThreadStaticAttribute/Overview/threadsafe2a.fs @@ -2,101 +2,31 @@ open System open System.Threading -type Example() = +type ThreadLocal() = [] - static val mutable private previous : double + static val mutable private requestId: string option - [] - static val mutable private sum : double - - [] - static val mutable private calls : int - - [] - static val mutable private abnormal : bool - - static let mutable totalNumbers = 0 - static let countdown = new CountdownEvent(1) - static let lockObj = obj () - let rand = Random() - - - 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." + static member PerformLogging() = + Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId}: Logging request {ThreadLocal.requestId.Value}") - member _.GetRandomNumbers() = - // Initialize ThreadStatic fields for this thread. - Example.previous <- 0.0 - Example.sum <- 0.0 - Example.calls <- 0 - Example.abnormal <- false + static member PerformDatabaseOperation() = + Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId}: Processing DB operation for request {ThreadLocal.requestId.Value}") - 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}" + static member ProcessRequest(reqId: obj) = + ThreadLocal.requestId <- Some(reqId :?> string) + ThreadLocal.PerformDatabaseOperation() + ThreadLocal.PerformLogging() - 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 +[] +let main _ = + let thread1 = Thread(ThreadStart(fun () -> ThreadLocal.ProcessRequest("REQ-001"))) + let thread2 = Thread(ThreadStart(fun () -> ThreadLocal.ProcessRequest("REQ-002"))) -let ex = Example() -Thread.CurrentThread.Name <- "Main" -ex.Execute() + thread1.Start() + thread2.Start() + thread1.Join() + thread2.Join() -// 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/threadsafe2a.vb b/snippets/visualbasic/VS_Snippets_CLR_System/system.threadstaticattribute/vb/threadsafe2a.vb index e8eda6493f4..e4864abfa3e 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,110 +2,43 @@ Option Strict On ' +Imports System Imports System.Threading -Public Class Example - Shared previous As Double - Shared sum As Double - Shared calls As Integer - 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() - ' Initialize ThreadStatic fields for this thread. - previous = 0.0 - sum = 0.0 - calls = 0 - abnormal = False + thread1.Join() + thread2.Join() - Dim result As Double = 0.0 + Console.WriteLine("Main thread execution completed.") + End Sub - 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 + Sub ProcessRequest(ByVal requestId As Object) + ' Assign the request ID to the thread-static field + _requestId = requestId.ToString() - 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. + ' Simulate request processing across multiple method calls + PerformDatabaseOperation() + PerformLogging() + End Sub + + Sub PerformDatabaseOperation() + Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId}: Processing DB operation for request {_requestId}") + End Sub + + Sub PerformLogging() + Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId}: Logging request {_requestId}") + End Sub + +End Module ' From 824f1083dc6b405bd27ee6359e974cc70731e745 Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Sat, 15 Mar 2025 08:09:39 -0700 Subject: [PATCH 4/6] Feedback --- .../System/ThreadStaticAttribute/Overview/threadsafe2a.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs b/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs index c7ae21e8592..137f024fe3f 100644 --- a/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs +++ b/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs @@ -33,12 +33,12 @@ static void ProcessRequest(object? requestId) static void PerformDatabaseOperation() { - Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId}: Processing DB operation for request {_requestId}"); + Console.WriteLine($"Thread {Environment.CurrentManagedThreadId}: Processing DB operation for request {_requestId}"); } static void PerformLogging() { - Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId}: Logging request {_requestId}"); + Console.WriteLine($"Thread {Environment.CurrentManagedThreadId}: Logging request {_requestId}"); } } // From 431d9f6a5ada295055e36ef07a97cb51442118d3 Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Sat, 15 Mar 2025 08:11:11 -0700 Subject: [PATCH 5/6] Feedback --- .../System/ThreadStaticAttribute/Overview/threadsafe2a.fs | 4 ++-- .../system.threadstaticattribute/vb/threadsafe2a.vb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/snippets/fsharp/System/ThreadStaticAttribute/Overview/threadsafe2a.fs b/snippets/fsharp/System/ThreadStaticAttribute/Overview/threadsafe2a.fs index 80420537fac..6e76d8a93f1 100644 --- a/snippets/fsharp/System/ThreadStaticAttribute/Overview/threadsafe2a.fs +++ b/snippets/fsharp/System/ThreadStaticAttribute/Overview/threadsafe2a.fs @@ -7,10 +7,10 @@ type ThreadLocal() = static val mutable private requestId: string option static member PerformLogging() = - Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId}: Logging request {ThreadLocal.requestId.Value}") + Console.WriteLine($"Thread {Environment.CurrentManagedThreadId}: Logging request {ThreadLocal.requestId.Value}") static member PerformDatabaseOperation() = - Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId}: Processing DB operation for request {ThreadLocal.requestId.Value}") + Console.WriteLine($"Thread {Environment.CurrentManagedThreadId}: Processing DB operation for request {ThreadLocal.requestId.Value}") static member ProcessRequest(reqId: obj) = ThreadLocal.requestId <- Some(reqId :?> string) 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 e4864abfa3e..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 @@ -33,11 +33,11 @@ Module Program End Sub Sub PerformDatabaseOperation() - Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId}: Processing DB operation for request {_requestId}") + Console.WriteLine($"Thread {Environment.CurrentManagedThreadId}: Processing DB operation for request {_requestId}") End Sub Sub PerformLogging() - Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId}: Logging request {_requestId}") + Console.WriteLine($"Thread {Environment.CurrentManagedThreadId}: Logging request {_requestId}") End Sub End Module From 9dfbae878bf5924b5933546f0ba0e100a42554a9 Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Sat, 15 Mar 2025 17:07:06 -0700 Subject: [PATCH 6/6] Style updates --- .../System/ThreadStaticAttribute/Overview/threadsafe2a.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs b/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs index 137f024fe3f..fcc8fd8c3d2 100644 --- a/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs +++ b/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs @@ -9,8 +9,8 @@ class Program static void Main() { - Thread thread1 = new Thread(ProcessRequest); - Thread thread2 = new Thread(ProcessRequest); + Thread thread1 = new(ProcessRequest); + Thread thread2 = new(ProcessRequest); thread1.Start("REQ-001"); thread2.Start("REQ-002");