码迷,mamicode.com
首页 > Web开发 > 详细

上传程序Dictionary 字典 哈希--多读一写锁ReaderWriterLock

时间:2017-02-13 18:50:14      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:turn   str   sel   --   man   ble   ack   nbsp   move   

//上传程序Dictionary 字典 哈希
/// <summary>
/// 车辆控制信息哈斯表,Key是终端号,Value是车辆信息控制对象
/// </summary>
private Dictionary<string, MsgObject> _MsgObjectHashMap = new Dictionary<string, MsgObject>();
public Dictionary<string, MsgObject> MsgObjectHashMap { get { return _MsgObjectHashMap; } }
//用法
//判断是否包含
if (LogicManager.MsgObjectHashMap.ContainsKey(key))
{
MsgObject controlObject = LogicManager.MsgObjectHashMap[key];
//删除
LogicManager.MsgObjectHashMap.Remove(key);//处理之后移除   
//添加
if (!LogicManager.MsgObjectHashMap.ContainsKey(msgObjeKey))
{
LogicManager.MsgObjectHashMap.Add(msgObjeKey, controlObject);
}
-----------
/// <summary>
/// 连接列表
/// </summary>
public Dictionary<string, CAPacket> CaSessionTable = new Dictionary<string, CAPacket>();

//读写锁
/// <summary>
/// 车辆控制信息哈斯表锁
/// </summary>
private ReaderWriterLock _MsgObjectHashMapLock = new ReaderWriterLock();
public ReaderWriterLock MsgObjectHashMapLock { get { return _MsgObjectHashMapLock; } }
//用法:
LogicManager.DriverInfoHashMapLock.AcquireReaderLock(5000);//设定5000毫秒 获取读锁
if (LogicManager.DriverInfoHashMapLock.IsReaderLockHeld) //判断一下在释放锁
{
LogicManager.DriverInfoHashMapLock.ReleaseLock(); //或.ReleaseReaderLock();
}
//更新驾驶员信息
LogicManager.DriverInfoHashMapLock.AcquireWriterLock(5000);
if (LogicManager.DriverInfoHashMapLock.IsWriterLockHeld)
{
LogicManager.DriverInfoHashMapLock.ReleaseWriterLock();
}

 

上传程序Dictionary 字典 哈希--多读一写锁ReaderWriterLock

标签:turn   str   sel   --   man   ble   ack   nbsp   move   

原文地址:http://www.cnblogs.com/rogge7/p/6394903.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!