using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Linq; [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable")]
public class SyncDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
private Dictionary<TKey, TValue> innerDict;
private ReaderWriterLockSlim readWriteLock; public SyncDictionary()
{
this.readWriteLock = new ReaderWriterLockSlim();
this.innerDict = new Dictionary<TKey, TValue>();
} public SyncDictionary(int capacity)
{
this.readWriteLock = new ReaderWriterLockSlim();
this.innerDict = new Dictionary<TKey, TValue>(capacity);
} public void Add(KeyValuePair<TKey, TValue> item)
{
using (new AcquireWriteLock(this.readWriteLock))
{
this.innerDict[item.Key] = item.Value;
}
} public void Add(TKey key, TValue value)
{
using (new AcquireWriteLock(this.readWriteLock))
{
this.innerDict[key] = value;
}
} public void Clear()
{
using (new AcquireWriteLock(this.readWriteLock))
{
this.innerDict.Clear();
}
} public bool Contains(KeyValuePair<TKey, TValue> item)
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.Contains<KeyValuePair<TKey, TValue>>(item);
}
} public bool ContainsKey(TKey key)
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.ContainsKey(key);
}
} public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
using (new AcquireReadLock(this.readWriteLock))
{
this.innerDict.ToArray<KeyValuePair<TKey, TValue>>().CopyTo(array, arrayIndex);
}
} public IEnumerator GetEnumerator()
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.GetEnumerator();
}
} IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.GetEnumerator();
}
} public bool Remove(TKey key)
{
bool isRemoved;
using (new AcquireWriteLock(this.readWriteLock))
{
isRemoved = this.innerDict.Remove(key);
}
return isRemoved;
} public bool Remove(KeyValuePair<TKey, TValue> item)
{
using (new AcquireWriteLock(this.readWriteLock))
{
return this.innerDict.Remove(item.Key);
}
} public bool TryGetValue(TKey key, out TValue value)
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.TryGetValue(key, out value);
}
} public int Count
{
get
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.Count;
}
}
} public bool IsReadOnly
{
get
{
return false;
}
} public TValue this[TKey key]
{
get
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict[key];
}
}
set
{
using (new AcquireWriteLock(this.readWriteLock))
{
this.innerDict[key] = value;
}
}
} public ICollection<TKey> Keys
{
get
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.Keys;
}
}
} public ICollection<TValue> Values
{
get
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.Values;
}
}
} private class AcquireReadLock : IDisposable
{
private ReaderWriterLockSlim rwLock;
private bool disposedValue; public AcquireReadLock(ReaderWriterLockSlim rwLock)
{
this.rwLock = new ReaderWriterLockSlim();
this.disposedValue = false;
this.rwLock = rwLock;
this.rwLock.EnterReadLock();
} public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
} protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue && disposing)
{
this.rwLock.ExitReadLock();
}
this.disposedValue = true;
}
} private class AcquireWriteLock : IDisposable
{
private ReaderWriterLockSlim rwLock;
private bool disposedValue; public AcquireWriteLock(ReaderWriterLockSlim rwLock)
{
this.rwLock = new ReaderWriterLockSlim();
this.disposedValue = false;
this.rwLock = rwLock;
this.rwLock.EnterWriteLock();
} public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
} protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue && disposing)
{
this.rwLock.ExitWriteLock();
}
this.disposedValue = true;
}
}
}

  

SyncDictionary的更多相关文章

随机推荐

  1. Linux基础命令---pgrep

    pgrep pgrep指令可以按名字或者其他属性搜索指定的进程,显示出进程的id到标准输出. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedo ...

  2. GoldenGate实时投递数据到大数据平台(4)- ElasticSearch 2.x

    ES 2.x ES 2.x安装 下载elasticSearch 2.4.5, https://www.elastic.co/downloads/elasticsearch 解压下载后的压缩包,启动ES ...

  3. PHP官方文档和phpstorm配置指南

    http://cn2.php.net/manual/zh/ phpstorm安装——>next——>…… 下载PHP.exe 地址:http://www.php.net/ 配置interp ...

  4. reids非关系性数据库

     1.Redis环境配置 下载安装地址: https://github.com/MicrosoftArchive/redis/releases 解压文件到指定的目录,D:\ChromeCoreDown ...

  5. ConvertUtils.register(new DateConverter(null), java.util.Date.class)使用

    在我们使用BeanUtils.copyProperties(dest,orig)将一个类的属性赋值给另一个类的时候 如果类中存在 Date类型的转换可能会报"no value specifi ...

  6. Eloquent JavaScript #07# Project: A Robot

    索引 Notes Excercise Measuring a robot Robot efficiency Persistent group 注释即笔记: const roads = [ " ...

  7. 在nginx的http模块下面,一个server就可以看做一个站点,配置形式大概是这样的:

    http { index index.php index.htm index.html; server { server_name www.site1.com; location / { # [... ...

  8. HTML <​canvas> testing with Selenium and OpenCV

    from: https://www.linkedin.com/pulse/html-canvas-testing-selenium-opencv-maciej-kusz Since HTML < ...

  9. v-bind属性的绑定

    v-bind:属性绑定: 当我们并没有使用v-bind使用的时候,突破不能显示出来,会提示错误,提示我们使用v-bind: 当我们使用v-bind时图片就可以显示: v-bind的简写是冒号: 使用v ...

  10. CSM与UEFI

    最近公司产品部购置一批新电脑,但是预装的win10不能保证兼容老平台软件,于是安装win7系统的任务就落到了我的手中. 观察参数,是8代的U,产品说运维说无能为力,装不了win7.我在网上搜了一下,是 ...