S
Steve - DND
I've seen many threads on Google about questions regarding stateless
classes, and thread synchronization. However, either I am dense, or I have
just not found the right thread, but I'm still not clear on how it all
works. I understand that if I have a static member variable such as a
collection, then I need to lock those operations as below, correct?
public class StaticClass {
private static HashTable m_Hash = new HashTable();
public static void AddToHash(string key, string value) {
lock (m_Hash) {
m_Hash.Add(key, value);
}
}
}
What about in the instance below, where there are variables local only to
the function being called. Do I need to worry about interference from other
threads?
public class StaticClass {
private static HashTable m_Hash = new HashTable();
public static void GetHash(int id) {
HashTable hash = null;
if (id > 0) {
hash = StaticClass.GetDataByID(id);
}
return hash;
}
}
Do I need to worry about locking the HashTable in that instance or is it
playing in it's own sandbox at this point? What if 2 or more users attempt
to call StaticClass.GetHash(id) at the same time? Will there be any
interference or waiting that takes place, or does each user get their own
little version of the function to run? This confusion mostly stems from the
belief(my belief) that stateless objects are just one instance of a class
waiting for calls, and that only one user could access a given function at
any time, but many of the posts I have read seem to indicate otherwise.
Also if anyone knows where I can find any good articles or books on the
underlying nature of how the CLR works it would be really appreciated. I
would love to find out how the whole system really ticks.
Thanks,
Steve
classes, and thread synchronization. However, either I am dense, or I have
just not found the right thread, but I'm still not clear on how it all
works. I understand that if I have a static member variable such as a
collection, then I need to lock those operations as below, correct?
public class StaticClass {
private static HashTable m_Hash = new HashTable();
public static void AddToHash(string key, string value) {
lock (m_Hash) {
m_Hash.Add(key, value);
}
}
}
What about in the instance below, where there are variables local only to
the function being called. Do I need to worry about interference from other
threads?
public class StaticClass {
private static HashTable m_Hash = new HashTable();
public static void GetHash(int id) {
HashTable hash = null;
if (id > 0) {
hash = StaticClass.GetDataByID(id);
}
return hash;
}
}
Do I need to worry about locking the HashTable in that instance or is it
playing in it's own sandbox at this point? What if 2 or more users attempt
to call StaticClass.GetHash(id) at the same time? Will there be any
interference or waiting that takes place, or does each user get their own
little version of the function to run? This confusion mostly stems from the
belief(my belief) that stateless objects are just one instance of a class
waiting for calls, and that only one user could access a given function at
any time, but many of the posts I have read seem to indicate otherwise.
Also if anyone knows where I can find any good articles or books on the
underlying nature of how the CLR works it would be really appreciated. I
would love to find out how the whole system really ticks.
Thanks,
Steve