472,378 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

locking question when using generic dictionary

1
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
}
}
}
Mar 23 '07 #1
0 1235

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Edward Diener | last post by:
Version 2.0 of the Python database API was written over 5 years ago, in 1999. While it has been used successfully by many implementations, there is no generic access into the data dictionary of...
0
by: Eyvind W Axelsen | last post by:
When I open an existing component containing an OleDbDataAdapter, or when I try to add an OleDbDataAdapter to a component, I receive the following error message. Up til today, this has worked...
12
by: Elhanan | last post by:
hi.. i wanted to build a Dictionary Classs that will load my own class called letter, i understood that i implement the IEquatable interface's equles method that then the dictionary would use...
4
by: Bill Woodruff | last post by:
< note : this message was sparked in part by comments by David Browne on a previous thread : "inserting an anonymous method as a value in a generic dictionary ?" : David had shown the use of...
3
by: =?Utf-8?B?YWluZHJlaQ==?= | last post by:
This code uses lazy initialization and double checked locking to read urls associated with a retailer from a map. Will the code work? I'm not sure how volatile applies to references. private...
6
by: Akula | last post by:
Does anyone know whether or not it is faster to lock a simple object, rather than a complex type? For example: Dictionary<string, SomeOtherClassdict = new Dictionary<string,...
2
by: =?Utf-8?B?RGF2aWQgTW9ycmlz?= | last post by:
I am trying to create a nested Dictionary and get an error that seems odd to me. Here is my declaration: private IDictionary<Guid, IDictionary<Guid, string>> myNestedDictionary = new...
9
by: eric | last post by:
I'm trying to define a dictionary whose value is an Generic Action<> delegate private Dictionary<string, List<Action<T>>> Any ideas on to how specify T or a different collection type more...
1
by: polastine | last post by:
Hi Anthony -- Anthony Jones wrote: OK, figured as much. I'm not forking any threads myself. 2.0, currently. Why? Is there a difference/advantage? I can probably go to 3.x if there's a...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.