ConcurrentDictionary和Dictionary
Is the ConcurrentDictionary thread-safe to the point that I can use it for a static cache?
问题:
Basically, if I want to do the following:
public class SomeClass
{
private static ConcurrentDictionary<..., ...> Cache { get; set; }
}
Does this let me avoid using lock
s all over the place?
解答:
Yes, it is thread safe and yes it avoids you using locks all over the place (whatever that means). Of course that will only provide you a thread safe access to the data stored in this dictionary, but if the data itself is not thread safe then you need to synchronize access to it of course. Imagine for example that you have stored in this cache a List<T>
. Now thread1 fetches this list (in a thread safe manner as the concurrent dictionary guarantees you this) and then starts enumerating over this list. At exactly the same time thread2 fetches this very same list from the cache (in a thread safe manner as the concurrent dictionary guarantees you this) and writes to the list (for example it adds a value). Conclusion: if you haven't synchronized thread1 it will get into trouble.
As far as using it as a cache is concerned, well, that's probably not a good idea. For caching I would recommend you what is already built into the framework. Classes such as MemoryCache for example. The reason for this is that what is built into the System.Runtime.Caching
assembly is, well, explicitly built for caching => it handles things like automatic expiration of data if you start running low on memory, callbacks for cache expiration items, and you would even be able to distribute your cache over multiple servers using things like memcached, AppFabric, ..., all things that you would can't dream of with a concurrent dictionary.
Is the MemoryCache class thread-safe like the ConcurrentDictionary is though? – michael
@michael, yes it is thread safe but absolutely the same remark stands true about synchronizing access to non thread-safe objects that you might be storing into this cache. – Darin Dimitrov
Oh, I understand that part. But, just so that other readers can understand I'm going to reiterate it. You're saying that both the ConcurrentDictionary
and MemoryCache
class are thread-safe, but the contents within are not guaranteed to be thread-safe. :) – michael
@michael, exactly, nicely put. My English is so poor that I can't express myself. – Darin Dimitrov
ConcurrentDictionary
internal class Program
{
private static void Main()
{
try
{
var dictionary = new ConcurrentDictionary<string, int>();
for (char c = 'A'; c <= 'F'; c++)
{
dictionary.TryAdd(c.ToString(), c - 'A' + );
} var iterationTask = new Task(() =>
{
foreach (var pair in dictionary)
{
Console.WriteLine(pair.Key + ":" + pair.Value);
}
}); var updateTask = new Task(() =>
{
foreach (var pair in dictionary)
{
dictionary[pair.Key] = pair.Value + ;
}
}); var addTask = new Task(() =>
{
for (char c = 'G'; c <= 'K'; c++)
{
//dictionary[c.ToString()] = c - 'A' + 1;
bool flag = dictionary.TryAdd(c.ToString(), c - 'A' + );
if (!flag)
{
Console.WriteLine("添加{0}失败", c);
}
else
{
Console.WriteLine("添加{0}成功", c);
}
}
}); var deleteTask=new Task(() =>
{
foreach (var pair in dictionary)
{
if (Convert.ToChar(pair.Key) <= 'F')
{
int number;
bool flag = dictionary.TryRemove(pair.Key, out number);
if (!flag)
{
Console.WriteLine("移除{0}失败", pair.Key);
}
else
{
Console.WriteLine("移除{0}成功", pair.Key);
}
}
}
}); updateTask.Start();
iterationTask.Start();
addTask.Start();
deleteTask.Start();
Task.WaitAll(updateTask, iterationTask,addTask,deleteTask); }
catch (Exception ex)
{
while (ex != null)
{
Console.WriteLine(ex.Message);
ex = ex.InnerException;
}
}
Console.ReadLine();
}
}
执行结果几乎每次都不相同,但是总能成功执行。
Dictionary
非线程安全的,代码执行的时候,会提示,集合已修改
internal class Program
{
private static void Main()
{
try
{
var dictionary = new Dictionary<string, int>();
for (char c = 'A'; c <= 'F'; c++)
{
dictionary.Add(c.ToString(), c - 'A' + );
} var iterationTask = new Task(() =>
{
foreach (var pair in dictionary)
{
Console.WriteLine(pair.Key + ":" + pair.Value);
}
}); var updateTask = new Task(() =>
{
foreach (var pair in dictionary)
{
dictionary[pair.Key] = pair.Value + ;
}
}); var addTask = new Task(() =>
{
for (char c = 'G'; c <= 'K'; c++)
{
dictionary.Add(c.ToString(), c - 'A' + );
}
}); var deleteTask = new Task(() =>
{
foreach (var pair in dictionary)
{
if (Convert.ToChar(pair.Key) <= 'F')
{
bool flag = dictionary.Remove(pair.Key);
if (!flag)
{
Console.WriteLine("移除{0}失败", pair.Key);
}
else
{
Console.WriteLine("移除{0}成功", pair.Key);
}
}
}
}); updateTask.Start();
iterationTask.Start();
addTask.Start();
deleteTask.Start();
Task.WaitAll(updateTask, iterationTask, addTask, deleteTask); }
catch (Exception ex)
{
while (ex != null)
{
Console.WriteLine(ex.Message);
ex = ex.InnerException;
}
}
Console.ReadLine();
}
}
线程不安全的方法
http://www.cnblogs.com/chucklu/p/4468057.html
https://stackoverflow.com/questions/51138333/is-this-concurrentdictionary-thread-safe
No; the dictionary could change between ContainsKey()
& TryAdd()
.
You should never call two methods in a row on ConcurrentDictionary
, unless you're sure you don't care if it changes between them.
Similarly, you can't loop through the dictionary, since it might change during the loop.
Instead, you should use its more-complex methods (like TryAdd()
, which will check and add in a single atomic operation.
Also, as you suggested, the entire dictionary might be replaced.
There is a specific section in the documentation that makes clear not everything is thread-safe in the ConcurrentDictionary<TKey, TValue>
:
All these operations are atomic and are thread-safe with regards to all other operations on the
ConcurrentDictionary<TKey, TValue>
class. The only exceptions are the methods that accept a delegate, that is,AddOrUpdate
andGetOrAdd
. For modifications and write operations to the dictionary,ConcurrentDictionary<TKey, TValue>
uses fine-grained locking to ensure thread safety. (Read operations on the dictionary are performed in a lock-free manner.) However, delegates for these methods are called outside the locks to avoid the problems that can arise from executing unknown code under a lock. Therefore, the code executed by these delegates is not subject to the atomicity of the operation.
So there are some general exclusions and some situations specific to ConcurrentDictionary<TKey, TValue>
:
- The delegates on
AddOrUpdate
andGetOrAdd
are not called in a thread-safe matter. - Methods or properties called on an explicit interface implementation are not guaranteed to be thread-safe;
- Extension methods called on the class are not guaranteed to be thread-safe;
- All other operations on public members of the class are thread-safe.
最后是用MemoryCache来处理?
https://stackoverflow.com/questions/21269170/locking-pattern-for-proper-use-of-net-memorycache
This is my 2nd iteration of the code. Because MemoryCache
is thread safe you don't need to lock on the initial read, you can just read and if the cache returns null then do the lock check to see if you need to create the string. It greatly simplifies the code.
const string CacheKey = "CacheKey";
static readonly object cacheLock = new object();
private static string GetCachedData()
{ //Returns null if the string does not exist, prevents a race condition where the cache invalidates between the contains check and the retreival.
var cachedString = MemoryCache.Default.Get(CacheKey, null) as string; if (cachedString != null)
{
return cachedString;
} lock (cacheLock)
{
//Check to see if anyone wrote to the cache while we where waiting our turn to write the new value.
cachedString = MemoryCache.Default.Get(CacheKey, null) as string; if (cachedString != null)
{
return cachedString;
} //The value still did not exist so we now write it in to the cache.
var expensiveString = SomeHeavyAndExpensiveCalculation();
CacheItemPolicy cip = new CacheItemPolicy()
{
AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes())
};
MemoryCache.Default.Set(CacheKey, expensiveString, cip);
return expensiveString;
}
}
EDIT: The below code is unnecessary but I wanted to leave it to show the original method. It may be useful to future visitors who are using a different collection that has thread safe reads but non-thread safe writes (almost all of classes under the System.Collections
namespace is like that).
Here is how I would do it using ReaderWriterLockSlim
to protect access. You need to do a kind of "Double Checked Locking" to see if anyone else created the cached item while we where waiting to to take the lock.
const string CacheKey = "CacheKey";
static readonly ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim();
static string GetCachedData()
{
//First we do a read lock to see if it already exists, this allows multiple readers at the same time.
cacheLock.EnterReadLock();
try
{
//Returns null if the string does not exist, prevents a race condition where the cache invalidates between the contains check and the retreival.
var cachedString = MemoryCache.Default.Get(CacheKey, null) as string; if (cachedString != null)
{
return cachedString;
}
}
finally
{
cacheLock.ExitReadLock();
} //Only one UpgradeableReadLock can exist at one time, but it can co-exist with many ReadLocks
cacheLock.EnterUpgradeableReadLock();
try
{
//We need to check again to see if the string was created while we where waiting to enter the EnterUpgradeableReadLock
var cachedString = MemoryCache.Default.Get(CacheKey, null) as string; if (cachedString != null)
{
return cachedString;
} //The entry still does not exist so we need to create it and enter the write lock
var expensiveString = SomeHeavyAndExpensiveCalculation();
cacheLock.EnterWriteLock(); //This will block till all the Readers flush.
try
{
CacheItemPolicy cip = new CacheItemPolicy()
{
AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes())
};
MemoryCache.Default.Set(CacheKey, expensiveString, cip);
return expensiveString;
}
finally
{
cacheLock.ExitWriteLock();
}
}
finally
{
cacheLock.ExitUpgradeableReadLock();
}
}
ConcurrentDictionary和Dictionary的更多相关文章
- ConcurrentDictionary与Dictionary 替换
本文导读:ASP.NET中ConcurrentDictionary是.Net4 增加的,相对于Dictionary的线程安全的集合, ConcurrentDictionary可实现一个线程安全的集合, ...
- ConcurrentDictionary 与 Dictionary
ASP.NET中ConcurrentDictionary是.Net4 增加的,与 Dictionary 最主要区别是, 前者是线程安全的集合,可以由多个线程同时并发读写Key-value. 那么 ...
- ConcurrentDictionary 对决 Dictionary+Locking
在 .NET 4.0 之前,如果我们需要在多线程环境下使用 Dictionary 类,除了自己实现线程同步来保证线程安全之外,我们没有其他选择. 很多开发人员肯定都实现过类似的线程安全方案,可能是通过 ...
- 浅谈ConcurrentDictionary与Dictionary
在.NET4.0之前,如果我们需要在多线程环境下使用Dictionary类,除了自己实现线程同步来保证线程安全外,我们没有其他选择.很多开发人员肯定都实现过类似的线程安全方案,可能是通过创建全新的线程 ...
- 改进ConcurrentDictionary并行使用的性能
上一篇文章“ConcurrentDictionary 对决 Dictionary+Locking”中,我们知道了 .NET 4.0 中提供了线程安全的 ConcurrentDictionary< ...
- ConcurrentDictionary<TKey, TValue>的AddOrUpdate方法
https://msdn.microsoft.com/zh-cn/library/ee378665(v=vs.110).aspx 此方法有一共有2个,现在只讨论其中一个 public TValue A ...
- ConcurrentDictionary并发字典知多少?
背景 在上一篇文章你真的了解字典吗?一文中我介绍了Hash Function和字典的工作的基本原理. 有网友在文章底部评论,说我的Remove和Add方法没有考虑线程安全问题. https://doc ...
- 1、C#中Hashtable、Dictionary详解以及写入和读取对比
在本文中将从基础角度讲解HashTable.Dictionary的构造和通过程序进行插入读取对比. 一:HashTable 1.HashTable是一种散列表,他内部维护很多对Key-Value键值对 ...
- Encountered an unexpected error when attempting to resolve tag helper directive '@addTagHelper' with value '"*, Microsoft.AspNet.Mvc.TagHelpers"'
project.json 配置: { "version": "1.0.0-*", "compilationOptions": { " ...
随机推荐
- Python实战(2)
在安装python第三方插件库的时候遇到了这个错误 遇到这种问题可以”转战“国内的第三方镜像,问题便可迎刃而解.例如豆瓣镜像——http://pypi.douban.com/simple/ 先安装ea ...
- python学习小结5:封装、继承、多态
面向对象程序设计中的类有三大特性: 继承,封装,多态 继承:以普通的类为基础建立专门的类对象 封装:对外部世界隐藏对象的工作细节 多态:可对不同类的对象使用同样的操作 在Python中类的继承定义基本 ...
- 编译时和运行时、OC中对象的动态编译机制
编译时 编译时顾名思义就是正在编译的时候.那啥叫编译呢?就是编译器帮你把源代码翻译成机器能识别的代码.(当然只是一般意义上这么说,实际上可能只是翻译成某个中间状态的语言.比如Java只有JVM识别的字 ...
- AvalonDock 2.0+Caliburn.Micro+MahApps.Metro实现Metro风格插件式系统(一)
随着IOS7由之前UI的拟物化设计变为如今的扁平化设计,也许扁平化的时代要来了,当然我们是不是该吐槽一下,苹果什么时候也开始跟风了,自GOOGLE和微软界面扁平化过后,苹果也加入了这一队伍. Aval ...
- Wireshark技巧-过滤规则和显示规则
Wireshark是一个强大的网络协议分析软件,最重要的它是免费软件. 过滤规则 只抓取符合条件的包,在Wireshark通过winpacp抓包时可以过滤掉不符合条件的包,提高我们的分析效率. 如果要 ...
- C# 该行已经属于另一个表 的解决方法[转]
该文转自:http://blog.sina.com.cn/s/blog_48e4c3fe0100nzs6.html DataTable dt = new DataTable(); dt = ds.Ta ...
- 如何使用Xcode6 调试UI,Reveal
实际测试需要使用IOS8并且32-bit的设备:具体打开调试的方法有三种: 1.底部调试菜单中: 2,debug菜单中 3.debug navigator 中
- 【转载】VMWare ESXi 5.0和vSphere Client安装和配置
免责声明: 本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除. 原文作者:张洪洋_ 原文地址:http://blog.sina.com.cn ...
- [CF]codeforces round 369(div2)
*明早起来再贴代码 A [题意] 给定n*5的方格 将横向的相邻两个变成+输出 [题解] ... B [题意] 一个n*n的正整数矩阵,有且仅有一个数为0 ,在这个位置填上一个数,使得每一列的和 每一 ...
- Unity3D研究院之脚本批量打包渠道包研究
原地址:http://www.xuanyusong.com/archives/2418#comments 最近在研究Unity3D脚本批量打包,比如在Android平台下各种不同分辨率和不同内存大小的 ...