CKTIS / C#
System.Threading.Volatile: praktická reference
System.Threading.Volatile: praktická reference: Volatile.Read a Volatile.Write zajišťují viditelnost a vhodné bariéry pro jedno místo paměti.
Verze: .NET API; verified against .NET 10 reference surface
Syntaxe
System.Threading.Volatile /* select a documented member or constructor */Volatile.Read a Volatile.Write zajišťují viditelnost a vhodné bariéry pro jedno místo paměti.
Použijte pro jednoduché příznaky publikované mezi vlákny bez potřeby složené operace.
Nečiní sekvenci read-modify-write atomickou; pro počítadla použijte Interlocked.
Viditelnost jednoho pole nevytvoří invariant více polí bez správného protokolu.
Úplný program skutečně používá System.Threading.Volatile; přesně očekávaný stdout je 11.
Příklady
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));
Výsledek
11
Úplný program skutečně používá `System.Threading.Volatile`; přesně očekávaný stdout je `11`.