CKTIS / C#
System.Threading.Volatile: practical reference
System.Threading.Volatile: practical reference: Volatile.Read and Volatile.Write provide visibility and appropriate barriers for one memory location.
Version: .NET API; verified against .NET 10 reference surface
Syntax
System.Threading.Volatile /* select a documented member or constructor */Volatile.Read and Volatile.Write provide visibility and appropriate barriers for one memory location.
Use them for simple flags published between threads when no compound operation is required.
They do not make a read-modify-write sequence atomic; use Interlocked for counters.
Visibility of one field does not establish a multi-field invariant without a proper protocol.
The complete program actually uses System.Threading.Volatile; its exact expected stdout is 11.
Examples
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;
var value = 0;
Volatile.Write(ref value, 11);
Console.WriteLine(Volatile.Read(ref value));
Result
11
The complete program actually uses `System.Threading.Volatile`; its exact expected stdout is `11`.