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

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

  •  
  •   bthulu · 2022-10-02 20:57:38 +08:00 · 1401 次点击
    这是一个创建于 542 天前的主题,其中的信息可能已经有所发展或是发生改变。
        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   ·   我们的愿景   ·   实用小工具   ·   3384 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:19 · PVG 19:19 · LAX 04:19 · JFK 07:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.