N
NvrBst
Is there a way to make Collections (Like SortedDictionary<T,K>) thread
safe with properties? For value types (IE the primative types) doing
the following makes them thread safe I think
private ReaderWriterSlim myLock = new ReaderWriterSlim();
private string myString;
public string MyString {
get { myLock.EnterReadLock(); try { return myString; } finally
{ myLock.ExitReadLock(); } }
set { myLock.EnterWriteLock(); try { myString = value; } finally
{ myLock.ExitWriteLock(); } }
}
For reference types (like collections) I'm unsure. If we changed the
above example from "string" to "SortedDictionary<string, string>" then
we call "MyString.Add(...)" the get property would be called but, if
my understanding is correct, the lock would unlock when the reference
to "myString" is returned, and then the "Add" method would be called?
If I want to not worry about locks while programing (with a collection
thats accessed by multiple threads) do I have to create a wrapper
classes that override all the methods I use from the collection class,
or is there a more elegant way?
It just seems like something that should be very common, and wondering
if there is a better solution than the one I see. If wrapping is what
I have to do, IE when making "myThreadSafeSortedDictionary", what
would I override exactly? ContainsKey / Add / [] / Count are what I
usally use? Does [] automatically call Add? Are there hidden ones I
need to do (Equals?)? Does properties, like Count, need to be done?
Thanks I know this post is a little long. I'm welcome to any
comments or suggestions even if its just on 1 part. Or if anyone
knows some websites I should be reading that answers some of my
questions I'd be greatful.
NB
safe with properties? For value types (IE the primative types) doing
the following makes them thread safe I think
private ReaderWriterSlim myLock = new ReaderWriterSlim();
private string myString;
public string MyString {
get { myLock.EnterReadLock(); try { return myString; } finally
{ myLock.ExitReadLock(); } }
set { myLock.EnterWriteLock(); try { myString = value; } finally
{ myLock.ExitWriteLock(); } }
}
For reference types (like collections) I'm unsure. If we changed the
above example from "string" to "SortedDictionary<string, string>" then
we call "MyString.Add(...)" the get property would be called but, if
my understanding is correct, the lock would unlock when the reference
to "myString" is returned, and then the "Add" method would be called?
If I want to not worry about locks while programing (with a collection
thats accessed by multiple threads) do I have to create a wrapper
classes that override all the methods I use from the collection class,
or is there a more elegant way?
It just seems like something that should be very common, and wondering
if there is a better solution than the one I see. If wrapping is what
I have to do, IE when making "myThreadSafeSortedDictionary", what
would I override exactly? ContainsKey / Add / [] / Count are what I
usally use? Does [] automatically call Add? Are there hidden ones I
need to do (Equals?)? Does properties, like Count, need to be done?
Thanks I know this post is a little long. I'm welcome to any
comments or suggestions even if its just on 1 part. Or if anyone
knows some websites I should be reading that answers some of my
questions I'd be greatful.
NB