I have a recursive algorithm that creates two new threads each time through. My tests crash with the following error when the array's length is 100k. I beleive this is exceeding the thread pool and/or running out of memory.
The question now, is how can I redesign the algorithm to not crash?
Test Authoring and Execution Framework: TE.ProcessHost.Managed[v5.3-1509] has stopped working
public class MyParamObj
{
public int[] MyArray;
public int MyIntOne;
public int MyIntTwo;
public static Create( int[] myArray, int myIntOne, int myIntTwo )
{
return new MyParamObj
{
MyArray = myArray,
MyIntOne = myIntOne,
MyIntTwo = myIntTwo
};
}
}
public class MyClass
{
public void Entry( int[] myArray)
{
int intOne = 0;
int intTwo = myArray.Length;
MyDelegate( MyParamObj.Create( myArray, intOne, intTwo) );
}
private void MyDelegate( object paramaObject )
{
var parameters = paramaObject as MyParamObjCreate;
if (parameters == null) throw new ArgumentNullException(nameof(parameters));
// just sample values
int intOne = 0;
int intTwo = 0;
int intThree = 0;
int intFour = 0
var threadOneParams = MyParamObj.Create( parameters.MyArray, intOne, intTwo );
var threadTwoParams = MyParamObj.Create( parameters.MyArray, intThree, intFour );
var threads = new Thread[2];
threads[0] = new Thread( MyDelegate );
threads[1] = new Thread( MyDelegate );
threads[0].Start(threadOneParams);
threads[1].Start(threadTwoParams);
threads[0].Join();
threads[1].Join();
//reorder elements within the array
}
}
Update 3
I think I am exceeding the thread pool's max limit. When I have an array that's 100k long and I'm recursive down to a size of 1, I'll have 100k+ threads I think. Maximum number of threads in a .NET app? So, I guess my next question, is how do I recursively do this operation while not exceeding the thread limit?