CKTIS / C#
System.Threading.Interlocked: odniesienie praktyczne
System.Threading.Interlocked: odniesienie praktyczne: Interlocked wykonuje atomowe odczyty-modyfikacje-zapisy na wspieranych lokalizacjach pamięci.
Wersja: .NET API; verified against .NET 10 reference surface
Składnia
System.Threading.Interlocked /* select a documented member or constructor */Interlocked wykonuje atomowe odczyty-modyfikacje-zapisy na wspieranych lokalizacjach pamięci.
Stosuj do prostych liczników, flag i compare-exchange bez pełnej blokady.
Atomowość pojedynczej operacji nie chroni wielopolowego invariantu.
Increment może zawinąć się bez wyjątku; CompareExchange zwraca poprzednią wartość.
Kompletny program rzeczywiście używa System.Threading.Interlocked; oczekiwany stdout to dokładnie 1.
Przykłady
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;
Console.WriteLine(Interlocked.Increment(ref value));
Wynik
1
Kompletny program rzeczywiście używa `System.Threading.Interlocked`; oczekiwany stdout to dokładnie `1`.