Solved

Using SyncLock to control access to a hastable

Posted on 2012-03-14
6
921 Views
Last Modified: 2012-08-13
Hi, I have the following code

Dim htb as hashtable = htbTrades.Clone
htbTrades.Clear

Open in new window


In this example htbTrades is a hashtable that is declared as a global variable and can be added to by other threads.  I want to make sure that no items can be added to the htbTrades hashtable between the Clone and Clear operations.  Is SyncLock what I should be using.  Will the following achieve my objective.

Dim htb as hashtable
SyncLock htbTrades.SyncRoot
        htb = htbTrades.Clone
        htbTrades.Clear()
End SyncLock

Open in new window


Thanks
0
Comment
Question by:nicksbell
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:lojk
ID: 37720401
http://msdn.microsoft.com/en-us/library/system.collections.hashtable.clone.aspx

Well - yes and no. It will only shallow copy the hashtable so a lot depends on what you are containing in the hashtable. Heres the best way i can explain that...

Imagine you have 10 cups on a table and each cup is connected to a Keyring via a peice of string. Cloning the hashtable merely adds another keyring and another 10 peices of string connected to the original cups.  If you want 10 more cups, then i think you may need to CopyTo instead.

If you are only storing value types (and/or strings) it should work fine but if you are using it to hold instances of complex types/classes you might need to check what you are trying to acheive is actually working correctly.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 37720565
You'd also have to place all ADDS to that HashTable within a similar SyncLock block.
0
 
LVL 9

Expert Comment

by:lojk
ID: 37720642
I'm with idlemind - in fact i wouldn't even expose it is a global variable at all rather a private one with a readonly property and a few public methods to proxy access to it (AddToHash, CopyHash etc) with the synclocks in place in there.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 2

Author Comment

by:nicksbell
ID: 37720768
Thanks for your replies.

lokj, re my first comment, my issue is not with the Clone method.  I know that works for what I am trying to do as I already have that in production.  I simply want to make sure that no items can be added to the hashtable after Clone and before Clear, as then they would be lost altogether.  I am not worried about the underlying objects contained in the hashtable as they are static and are never updated, only new ones are added.

Idle_mind, ok, I didn't realise that but that makes sense.

lokj, just to clarify, are you suggesting I create a class that inherits the hashtable class and then expose a public AddToHash method (which would handle the synclock) and all other code would call AddToHash?  I guess that makes a lot of sense too as it stops the need for synclocks all over the place.
0
 
LVL 9

Assisted Solution

by:lojk
lojk earned 250 total points
ID: 37721120
not explicity saying an inherited class here more just a couple of helper methods in the global module to manage the utilisation of Idleminds point but i was going to suggest it and avoided it for simplicity reasons.

I think i would just create a Shared Class (module - not getting into that discussion now) with an private instance of the hashtable  and Get,Add,Delete,Clone,Clear methods that all implement the sync methods - it would always be fairly thread safe then.

MySafeHashTable.AddNewItem("key",Item);
Item = MySafeHashTable.GetItem("key");
MySafeHashTable.FlushAll();

The knock-on benefit to this approach would be by removing that global hash , you'd also weed out all those instances in your code where it's used allowing a nice simple hit list of places to change to the new methods.
0
 
LVL 2

Author Closing Comment

by:nicksbell
ID: 37721344
Thanks, very useful
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now