V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
bthulu
V2EX  ›  .NET

ConcurrentDictionary 有什么办法确保一次添加多项的原子性吗?

  •  
  •   bthulu · 2022-10-02 20:57:38 +08:00 · 1466 次点击
    这是一个创建于 605 天前的主题,其中的信息可能已经有所发展或是发生改变。
        private readonly ConcurrentDictionary<int, string> Dict = new();
    
        // 如何保证原子性?
        public void AddMore(IEnumerable<(int, string)> items)
        {
            foreach ((int key, string? value) in items)
            {
                Dict.TryAdd(key, value);
            }
        }
    
    liuhan907
        1
    liuhan907  
       2022-10-03 00:22:37 +08:00
    没有办法。换个常规字典自己 lock 吧。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1015 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 21:14 · PVG 05:14 · LAX 14:14 · JFK 17:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.