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基础命令---添加用户useradd

    useradd 创建新的系统用户,useradd指令只能以管理员的身份运行,创建的用户都在“/etc/passwd”文件中.当不加-D参数,useradd指令使用命令列来指定新帐号的设定值and使用系 ...

  2. WebSocket和long poll、ajax轮询的区别,ws协议测试

    WebSocket和long poll.ajax轮询的区别,ws协议测试 WebSocket是HTML5出的东西(协议),也就是说HTTP协议没有变化,或者说没关系,但HTTP是不支持持久连接的(长连 ...

  3. linux test

    some test .在登录Linux时,一个具有唯一进程ID号的shell将被调用,这个ID是什么(b) A.NID B.PID C.UID C.CID .下面那个用户存放用户密码信息(b) A./ ...

  4. [转载]SQL Server中的事务与锁

    了解事务和锁 事务:保持逻辑数据一致性与可恢复性,必不可少的利器. 锁:多用户访问同一数据库资源时,对访问的先后次序权限管理的一种机制,没有他事务或许将会一塌糊涂,不能保证数据的安全正确读写. 死锁: ...

  5. 浏览收藏夹 看到了hadoop3.0 的改动

    大数据非常前卫的it网站   https://www.iteblog.com/archives/tag/hadoop/ 1.多个服务的默认端口被改变 在此之前,多个Hadoop服务的默认端口都属于Li ...

  6. c#测试执行时间的方法

    获取当前实例测量出来的总的运行时间 Stopwatch sp = new Stopwatch(); sp.Start(); //要测试的代码块 sp.Stop(); Console.WriteLine ...

  7. await

    单个的task await task 多个await asyncio.wait(tasks)

  8. linux操作文件和文件夹

    rm filerm -rf folder如将/test1目录下的file1复制到/test3目录,并将文件名改为file2,可输入以下命令:cp /test1/file1 /test3/file2 如 ...

  9. The POM for XXX is invalid, transitive dependencies (if any) will not be available解决方案

    今天,某个开发的环境在编译的时候提示警告The POM for XXX is invalid, transitive dependencies (if any) will not be availab ...

  10. 零基础Python爬虫实现(爬取最新电影排行)

    提示:本学习来自Ehco前辈的文章, 经过实现得出的笔记. 目标网站 http://dianying.2345.com/top/ 网站结构 要爬的部分,在ul标签下(包括li标签), 大致来说迭代li ...