CKTIS / C#
System.Collections.Concurrent.ConcurrentDictionary: referencia práctica
System.Collections.Concurrent.ConcurrentDictionary: referencia práctica: ConcurrentDictionary<TKey,TValue> permite lecturas concurrentes y operaciones atómicas sobre entradas individuales de clave y valor.
Versión: .NET API; verified on .NET 10.0.14
Sintaxis
System.Collections.Concurrent.ConcurrentDictionary /* select a documented member or constructor */ConcurrentDictionary<TKey,TValue> permite lecturas concurrentes y operaciones atómicas sobre entradas individuales de clave y valor.
Usa AddOrUpdate, GetOrAdd y TryUpdate en mapas compartidos que deban cambiar de forma segura entre hilos.
Las fábricas de valores pueden ejecutarse varias veces fuera de los bloqueos internos; evita efectos secundarios no repetibles.
Un método atómico no convierte en atómica una secuencia de métodos y no se admite una clave null.
El programa completo usa realmente System.Collections.Concurrent.ConcurrentDictionary; su stdout esperado exacto es 1\n2.
Ejemplos
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 map = new ConcurrentDictionary<string, int>();
Console.WriteLine(map.AddOrUpdate("x", 1, (_, old) => old + 1));
Console.WriteLine(map.AddOrUpdate("x", 1, (_, old) => old + 1));
Resultado
1 2
El programa completo usa realmente `System.Collections.Concurrent.ConcurrentDictionary`; su stdout esperado exacto es `1\n2`.