并发字典正确使用

编程入门 行业动态 更新时间:2024-10-26 12:22:58
本文介绍了并发字典正确使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是正确的思维,这是正确的使用并发字典

Am I right in thinking this is the correct use of a Concurrent Dictionary

private ConcurrentDictionary<int,long> myDic = new ConcurrentDictionary<int,long>(); //Main thread at program startup for(int i = 0; i < 4; i++) { myDic.Add(i, 0); } //Seperate threads use this to update a value myDic[InputID] = newLongValue;

我没有锁等和我只是更新在字典中,即使多个线程可能会试图做同样的价值。

I have no locks etc and am just updating the value in the dictionary even though multiple threads might be trying to do the same.

推荐答案

这取决于你所说的线程安全的意思。

It depends on what you mean by thread-safe.

从MSDN - 如何:添加和删除项目从ConcurrentDictionary :

From MSDN - How to: Add and Remove Items from a ConcurrentDictionary:

ConcurrentDictionary&LT; TKEY的,TValue&GT; 是专为多线程方案。您不必使用锁在code从集合添加或删除项目。然而,它始终是可能的一个线程来检索一个值,和另一个线程来给予相同的密钥的新值立即更新集合

ConcurrentDictionary<TKey, TValue> is designed for multithreaded scenarios. You do not have to use locks in your code to add or remove items from the collection. However, it is always possible for one thread to retrieve a value, and another thread to immediately update the collection by giving the same key a new value.

因此​​,有可能得到一个项目在字典中的值的不一致的视图的

So, it is possible to get an inconsistent view of the value of an item in the dictionary.

更多推荐

并发字典正确使用

本文发布于:2023-11-10 01:54:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574034.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典   正确

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!