是一个LINQ查询ConcurrentDictionary值线程? [英] Is a linq query to ConcurrentDictionary Values threadsafe?

查看:448
本文介绍了是一个LINQ查询ConcurrentDictionary值线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有以下的code:

  ConcurrentDictionary<很久很久> myDict =新ConcurrentDictionary<很久很久>();
 

通常由关键每次访问是线程安全的,但同时也是以下LINQ查询线程安全的? 我还没有发现在这个文档什么: <一href="http://msdn.microsoft.com/en-us/library/dd287226.aspx">http://msdn.microsoft.com/en-us/library/dd287226.aspx

 如果myDict.Values​​.Any(X =&GT;!x.HasPaid))
{
  返回false
}
 

解决方案

校正......我不知道,当你访问的价值属性。它是线程安全的对象本身的使用LINQ时。


LINQ将使用GetEnumerator方法来itterate的项目。

直接从 MSDN

  

普查员从字典返回是安全的同时使用与读取和写入到字典中,但它不会重新present片刻即时快照的字典。通过枚举可能包含的GetEnumerator后的字典进行修改暴露的内容被称为

 如果myDict.Any(X =&GT;!x.Value.HasPaid))
{
  返回false
}
 

let's say I have the following code:

ConcurrentDictionary<long, long> myDict= new ConcurrentDictionary<long, long>();

Normally every access by key is threadsafe, but is also the following linq query threadsafe? I have not found anything in the docs: http://msdn.microsoft.com/en-us/library/dd287226.aspx

if myDict.Values.Any(x => !x.HasPaid))
{
  return false
}

解决方案

Correction... I'm not sure when you are accessing the Values property. It is thread safe when using LINQ on the object itself.


LINQ will use the GetEnumerator method to itterate the items.

Straight from MSDN

The enumerator returned from the dictionary is safe to use concurrently with reads and writes to the dictionary, however it does not represent a moment-in-time snapshot of the dictionary. The contents exposed through the enumerator may contain modifications made to the dictionary after GetEnumerator was called

if myDict.Any(x => !x.Value.HasPaid))
{
  return false
}

这篇关于是一个LINQ查询ConcurrentDictionary值线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆