While using a global integer named GlobalID
that I use as a counter,
the following code works fine:
MyFunction(myClass thisClass, Int ID)
{
ListOfMyClass.add(thisClass)
If (thisClass.IsPropertyValueAboveZero)
{
ID = GlobalID;
}
GlobalID++;
Foreach (class someClass in ListOfClasses)
{
MyClass newClass = new MyClass(GlobalID, ID)
MyFunction(newClass, ID) //Recursive Call
}
}
I'm basically using GlobalID
as a incrementing counter for each time the function is called. The counter is assigned a starting position for the first time it executes. I'm using a global variable because I wanted to make sure the ID is accurately increased for each pass regardless of execution entering or leaving the recursive call. This function is called (the first time....) from a ForEach loop that assigns the start position of global variable
My goal is to use a Parallel.ForEach for the initial call instead of the regular For Each loop. My problem deals with the counter. I don't know how to manage that counter within multiple threads. If I pass it as a variable to the function, I believe I will have a inaccurate lower / used number leaving the recursive loop. The global variable ensures the next number is higher than the previous number. The thisClass.IsPropertyValueAboveZero
is just a arbitrary means of describing action based on a conditional statement. It has no meaningful reference to the rest of the code.
If I have multiple threads that have different starting positions for their counter, how do I accomplish making this thread safe? The only way I see at the moment is manually writing multiple versions of the same function and counter and using TaskFactory