dotnet / dotnet/dotnet-api-docs

Examples output is incorrect

Open
#9,629 1 comment 0 reactions 0 assignees View on GitHub
area-System.Threading Pri3
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

The Example out is reverse order of what it normally is. Actually, both outputs are acceptable since the thread scheduler could cause a race condition, as the example output displays, switching to the second worker thread before the first even though the first thread's Task.Delay(100) should complete before the second.

Documented output:
// Exiting AsyncMethodB.
// Expected 'Value 2', got 'Value 2', ThreadLocal value is ''
// Exiting AsyncMethodB.
// Expected 'Value 1', got 'Value 1', ThreadLocal value is ''

Actual Output:
// Exiting AsyncMethodB.
// Expected 'Value 1', got 'Value 1', ThreadLocal value is ''
// Exiting AsyncMethodB.
// Expected 'Value 2', got 'Value 2', ThreadLocal value is ''

Additional explanation of why the ThreadLocal value is unset would also be useful.
Showing an equivalent decomposition showing the thread spawning abstraction:
```
static async Task AsyncMethodB(string expectedValue)
{
Console.WriteLine("Entering AsyncMethodB.");
Console.WriteLine(" Expected '{0}', AsyncLocal value is '{1}', ThreadLocal value is '{2}'",
expectedValue, _asyncLocalString.Value, _threadLocalString.Value);
await Task.Run(()=>
{
Task.Delay(100).Wait();
Console.WriteLine("Exiting AsyncMethodB.");
Console.WriteLine(" Expected '{0}', got '{1}', ThreadLocal value is '{2}'",
expectedValue, _asyncLocalString.Value, _threadLocalString.Value);
}
}

```
Then stating the reason ThreadLocal is empty is because : "The _threadLocalString.Value is being accessed from a new ThreadPool worker thread and the Value has not been set in this thread."

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.