-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Update ThreadStatic example #11094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update ThreadStatic example #11094
Conversation
Learn Build status updates of commit 30943f6: ✅ Validation status: passed
For more details, please refer to the build report. For any questions, please:
|
Learn Build status updates of commit cc9d1be: ✅ Validation status: passedFor more details, please refer to the build report. For any questions, please:
|
I think this is a poor sample for ThreadStatics to start with. It does not demonstrate a situation where ThreadStatic must be used (local variable would work just fine - all uses are in scope of a single method) and it has a bunch of irrelevant clutter. With help of ChatGPT, I would modify the sample to something like this: using System;
using System.Threading;
class Program
{
[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}");
}
} |
@jkotas Thanks. Used Copilot to create F# and VB.Net versions. |
Learn Build status updates of commit 0afabb5: ✅ Validation status: passedFor more details, please refer to the build report. For any questions, please:
|
I Like Jan Kotas' GPT code, but VS build raises two CA1840 Messages so after applying the analyzer fixes and Code Cleanup, imho the C# code should become
|
Learn Build status updates of commit 431d9f6: ✅ Validation status: passedFor more details, please refer to the build report. For any questions, please:
|
@AaronRobinsonMSFT et al, thanks for adopting the CA1840 suggestions in all lang flavours, but I noticed you declined the "var" changes [the long-form code looked tired to me].
so this note is to ridicule such comment since the doc-style edict is csharp_style_var_foo = false to disable var in ALL cases (i.e. I quarrel with the assertion "permit var only when the type is clear" since var seems verboten in every case). If var is so toxic, perhaps you should amend that comment to match? |
Learn Build status updates of commit 9dfbae8: ✅ Validation status: passedFor more details, please refer to the build report. For any questions, please:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
Summary
The examples provided an initialization of
ThreadStatic
fields, which is a bad example for how they should be used. Removed that and made the example explicitly initialize them for each thread.