CKTIS / C#
System.Threading.Volatile: praktična referenca
System.Threading.Volatile: praktična referenca: Volatile.Read in Volatile.Write zagotavljata vidnost in ustrezne pregrade za eno pomnilniško mesto.
Različica: .NET API; verified against .NET 10 reference surface
Sintaksa
System.Threading.Volatile /* select a documented member or constructor */Volatile.Read in Volatile.Write zagotavljata vidnost in ustrezne pregrade za eno pomnilniško mesto.
Uporabite za preproste zastavice med nitmi, kadar sestavljena operacija ni potrebna.
Zaporedja read-modify-write ne naredita atomskega; za števce uporabite Interlocked.
Vidnost enega polja brez ustreznega protokola ne vzpostavi invarianta več polj.
Celoten program dejansko uporabi System.Threading.Volatile; natančno pričakovani stdout je 11.
Primeri
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));
Rezultat
11
Celoten program dejansko uporabi `System.Threading.Volatile`; natančno pričakovani stdout je `11`.