Dudeman3000
August 5th, 2010, 12:26 PM
I’ve posted this question on a couple forums. Any threading guru among us know the answer?
So I have a method that gets a Dictionary of List<myObj>, then cycles through the keys of the dictionary and passes each List<myObj> to a separate thread.
Here is some Code / Psuedo-Code:
public static void ProcessEntries() {
Dictionary<string, List<myObj>> myDictionary = GetDictionary();
foreach(string key in myDictionary.keys)
{
List<myObj> myList = myDictionary[key];
Thread myThread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate() {
ProcessList(myList);
}
myThread.Start();
}
}
public static void ProcessList(List<myObj> myList) {
// Process entries
// read-only operations on myList
}
The problem is that during execution of ProcessList the myList parameter simply does not remain static – it changes.
I have looped through the list in ProcessEntries() before kicking off the thread, and then immediately inside the thread / ProcessList() method, and I've found the results to be different.
I have since solved the problem (I think!) by making the Dictionary variable global and retrieving the list within the ProcessList() function, but I find this not to be ideal. Using the [ThreadStatic] property is next on the list of possible fixes.
What I really want to know is why does the myList object change inside ProcessList(), presumably when the myList object is re-assigned in ProcessEntries() ? Are these not two different Lists in memory? If all parameter passing is by value by default, why does the ProcessList() function not have a local copy of myList ?
Is there a way to specify that you want to pass a parameter to a thread and not have it be altered by the parent thread or other threads during execution? (This would be similar to the [ThreadSafe] attribute for global variables)
Is there an important distinction between primitive type thread parameters and custom object thread parameters that I’m unaware of?
So I have a method that gets a Dictionary of List<myObj>, then cycles through the keys of the dictionary and passes each List<myObj> to a separate thread.
Here is some Code / Psuedo-Code:
public static void ProcessEntries() {
Dictionary<string, List<myObj>> myDictionary = GetDictionary();
foreach(string key in myDictionary.keys)
{
List<myObj> myList = myDictionary[key];
Thread myThread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate() {
ProcessList(myList);
}
myThread.Start();
}
}
public static void ProcessList(List<myObj> myList) {
// Process entries
// read-only operations on myList
}
The problem is that during execution of ProcessList the myList parameter simply does not remain static – it changes.
I have looped through the list in ProcessEntries() before kicking off the thread, and then immediately inside the thread / ProcessList() method, and I've found the results to be different.
I have since solved the problem (I think!) by making the Dictionary variable global and retrieving the list within the ProcessList() function, but I find this not to be ideal. Using the [ThreadStatic] property is next on the list of possible fixes.
What I really want to know is why does the myList object change inside ProcessList(), presumably when the myList object is re-assigned in ProcessEntries() ? Are these not two different Lists in memory? If all parameter passing is by value by default, why does the ProcessList() function not have a local copy of myList ?
Is there a way to specify that you want to pass a parameter to a thread and not have it be altered by the parent thread or other threads during execution? (This would be similar to the [ThreadSafe] attribute for global variables)
Is there an important distinction between primitive type thread parameters and custom object thread parameters that I’m unaware of?