C# Dictionary 线程安全指南:多线程下操作 Dictionary<string, DateTime> 的加锁策略

在讲述这个之前,我们先要弄清楚

为什么需要加锁

竞态条件示例

cs 复制代码
// 以下操作在多线程中都需要保护:
Dictionary<string, DateTime> dict = new Dictionary<string, DateTime>();

// 线程A
if (!dict.ContainsKey("ip1"))  // 检查不存在

// 此时线程B也检查同一个key,也不存在

// 线程A
dict.Add("ip1", time);  // 成功添加

// 线程B
dict.Add("ip1", time);  // 异常!键已存在

在C#中,当你在操作Dictionary<string, DateTime>这样的集合时,是否会加锁主要取决于你使用的是哪种类型的Dictionary。在.NET框架中,有两种主要的Dictionary实现:

  1. ‌**System.Collections.Generic.Dictionary<TKey, TValue>** ‌:这是非线程安全的。这意味着如果你在多线程环境下同时对同一个Dictionary实例进行写操作(如添加、删除、修改元素),你需要自己手动管理线程安全。

  2. ‌**System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>** ‌:这是线程安全的。ConcurrentDictionary<TKey, TValue>提供了内置的线程安全机制,允许多个线程同时访问和修改集合而不需要额外的锁。

如果你使用的是System.Collections.Generic.Dictionary<TKey, TValue>

如果你决定使用普通的Dictionary,并且需要在多线程环境下安全地操作它,你有以下几种选择:

新手可能认为 Mutex 更"强大"

cs 复制代码
public readonly static Mutex mutex = new Mutex(); // 其实过重
而有经验的开发者会用:
cs 复制代码
private static readonly object _lockObj = new object();
lock(_lockObj) { ... }

Mutexlock 更重量级:

  • Mutex:内核级对象,上下文切换开销大

  • lock:用户态同步,性能更好

cs 复制代码
public static Dictionary<string, DateTime> RemoteLinkStatue = new Dictionary<string, DateTime>();

/// <summary>
/// 读取操作加锁,跨线程
/// </summary>
public readonly static Mutex readRemoteLinkStatueMutex = new Mutex();

App.readRemoteLinkStatueMutex.WaitOne();
try 
{
    if (App.RemoteLinkStatue.ContainsKey(remoteIp)) 
        App.RemoteLinkStatue[remoteIp] = DateTime.Now;
    else 
        App.RemoteLinkStatue.Add(remoteIp, DateTime.Now);
}
finally 
{
    App.readRemoteLinkStatueMutex.ReleaseMutex();
}

更好的替代方案

方案1:使用 lock(推荐)

你可以使用lock语句来确保对字典的访问是线程安全的。例如:

cs 复制代码
private static readonly object _dictionaryLock = new object();

lock(_dictionaryLock) 
{
    if (App.RemoteLinkStatue.ContainsKey(remoteIp)) 
        App.RemoteLinkStatue[remoteIp] = DateTime.Now;
    else 
        App.RemoteLinkStatue.Add(remoteIp, DateTime.Now);
}
方案2:使用 ConcurrentDictionary(最佳)

使用ConcurrentDictionary<TKey, TValue> ‌:如果你经常需要在多线程环境中使用字典,并且对性能有较高要求,可以考虑使用ConcurrentDictionary

cs 复制代码
private static ConcurrentDictionary<string, DateTime> _myConcurrentDictionary = new ConcurrentDictionary<string, DateTime>();

public void AddItem(string key, DateTime value)
{
    // 无需手动加锁,方法本身是原子的、线程安全的
    _myConcurrentDictionary.TryAdd(key, value);
}

// 原子性操作
RemoteLinkStatus.AddOrUpdate(remoteIp, DateTime.Now, (key, oldValue) => DateTime.Now);

总结**:**

对于纯跨线程的场景,使用 Mutex过度设计

  • 单线程环境或只读访问 -> 使用 Dictionary,无需锁。

  • 如果你的应用是多线程的,并且对性能有较高要求,推荐使用ConcurrentDictionary<TKey, TValue>

  • 如果出于某种原因你不得不使用普通的Dictionary<TKey, TValue>,确保通过适当的同步机制(如锁)来保护对字典的访问。

选择合适的集合类型和同步策略对于确保应用程序的稳定性和性能至关重要

相关推荐
要开心吖ZSH16 小时前
一文搞懂:JDK 21 虚拟线程 vs N种异步方案,到底该怎么选?
java·数据库·jdk·虚拟线程
-银雾鸢尾-18 小时前
C#中的拓展方法
开发语言·c#
guodingdingh21 小时前
软件开发工作问题总结0718
java·开发语言·数据库
德迅云安全-上官1 天前
DDoS 安全防护在企业安全防护中的作用
安全·ddos
咖啡八杯1 天前
GoF设计模式——解释器模式
java·后端·spring·设计模式
优橙教育1 天前
5G网优培训 vs Java开发:转行选哪个?
java·开发语言·5g
糖果店的幽灵1 天前
【DeepAgents 从入门到精通】Context Management 上下文管理
java·人工智能·后端·spring·中间件·langgraph·deepagents
腻害兔1 天前
【若依项目-产品经理视角】RuoYi-Vue-Pro 源码拆解:字典、短信、邮件、通知——后台系统的“基础设施四件套“!
java·前端·vue.js·产品经理·ai编程
碎光拾影1 天前
ARM交叉工具链各工具作用及IMX6ULL平台LED+蜂鸣器裸机程序实现
java·开发语言·数据库
Miao121311 天前
微服务 API 测试实践:海外某民宿平台如何构建模式驱动测试基础设施
java·开发语言