Hello,
I have a class MyEmbededList contains a generic dictionary, the value field is actually the MyEmbededList type as well.
There is another class need to access and manipulate a list of MyEmbededList (please refer to the MyTestClass below).
I am not sure whether I implements the right locking mechanism here and hope someone can give me some advices.
I have provided some codes for these two classes below. My questions are:
1. Am I doing the right locking in the TestMehod in the TestClass below? I try to avoid locking the m_topList too long so try to lock the child list.After obtain the embededList that I need to modify then just lock that embededList. However, I am not sure whether whether the implementation of the locking is correct or not...
2. In such scenario, the locking is implemented in the TestClass, I also have locking in some method in MyEmbededList, for example, the AddNextLevelListItem method. If the TestClass will call the AddNextLevelListItem, should the locking be done by the TestClass and remove the locking in the MyEmbededList? or should I keep the locking in both? any drawbacks?
Thank you very much
public class MyEmbededList
{
private string m_name;
private Dictionary<string, MyEmbededList> m_nextLevelList;
priviate bool dummyStatus;
public Dictionary<string, MyEmbededList> NextLevelList
{
get
{
return m_nextLevelList;
}
set
{
m_nextLevelList= value;
}
}
public MyEmbededList(string _name)
{
m_name = _name;
m_nextLevelList = new Dictionary<string, MyEmbededList>();
}
public MyEmbededList GetNextLevelListItem(string _key)
{
MyEmbededList subList = null;
lock (((IDictionary)m_nextLevelList ).SyncRoot)
{
m_nextLevelList .TryGetValue(_key, out subList );
}
return subList ;
}
public void AddNextLevelListItem(string _key, MyEmbededList _emList)
{
lock (((IDictionary)m_nextLevelList ).SyncRoot)
{
if (!m_nextLevelList .ContainsKey(_key))
m_nextLevelList [_key] = _emList;
}
}
}
public static class MyTestClass
{
private static Dictionary<string, MyEmbededList > m_topList = new Dictionary<string,MyEmbededList >();
public static void TestMehod(string[] _keys)
{
MyEmbededList letterDic = null;
Dictionary<string, MyEmbededList > dic = m_topList;
// the goal is to get the deppest possible MyEmbededList and update its
// for example, if _keys = {"1", "2", "5"}
// we get the MyEmbededList (e.g. list1) from m_topList["1"]
// then get the next level MyEmbededList (e.g. list 2) from list1.m_nextLevelList["2"]
// then get the next level MyEmbededList (e.g. list 3) from list2.m_nextLevelList["5"]
// so list 3 is the list that I want to update the properties (e.g. dummyStatus)
// if list 3 not found, then return the previous found list (e.g. list 2) and update the
// properties of list 2 instead
for (int i = 0; i < _keys.Length; i++)
{
string key= _keysIdea;
lock (((IDictionary)dic).SyncRoot)
{
if (dic.ContainsKey(key))
{
letterDic = dic[key];
dic = letterDic.NextLevelList;
}
else
{
// TODO
break;
}
}
} // end for loop
lock (letterDic)
{
// update some properties for this object
}
}
}