dotnet / dotnet/dotnet-api-docs

`SpinWait` example ignores compiler HW/optimization

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

Description

If I am not wrong example:
- [page](https://learn.microsoft.com/en-us/dotnet/api/system.threading.spinwait?view=net-7.0#examples)
- [src](https://github.com/dotnet/dotnet-api-docs/blob/13d97c7e536ce822c000883dba873a79d5d6e841/snippets/csharp/System.Threading/SpinWait/Overview/spinwait.cs)

shows incorrect usage. Examples should be complete, understandable and most importantly correct.

Since `someBoolean` is accessed by multiple threads, it must be provided with synchronization manner for `r/w` ops. Similar [example](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/volatile#example) can be seen in _volatile (C# Reference)_ page. This one explicitly points out need for synchronization between threads.

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.