Hi, everybody I have a some computation program. Now, this program is a single-thread, I need to enhance it to do it multi-thread. In general, program computes, dynamic evolution of thermal circuit (Some configured types of different involved elements (Tube, Pump, Active zone, and its connection), for each of it for every time step program calculates diffusion equations (http://en.wikipedia.org/wiki/Diffusion%5Fequation)) Problem is, on every time steps each involved element could be handled separately, but on every new time steps all elements should synchronize each other. I've decided to do it by starting on each time step one Thread per element, to solve it.
private void SolveElementDynamic(object element)
{
if (element is PJunction)
{
((PJunction)element).SolveDynamic();
}
else
if (element is PElementData)
{
((PElementData)element).SolveDynamic();
}
}
public void SolveDynamic()
{
ParameterizedThreadStart threadStart = new ParameterizedThreadStart(SolveElementDynamic);
for (int i = 0; i < _elementDataCollection.Count; i++)
{
_threadArray.Add(new Thread(threadStart));
}
int j = 0;
foreach (object element in _elementDataCollection)
{
((Thread)_threadArray[j]).Start(element);
j++;
}
}
But it seems that creating new Thread is too expensive in comparison with calculating one element per step. I've tried to create Thread array, only one time at the start of the computation. But unfortunately It is seems that Thread object can't be started more than one times?
// Thread's array created before
public void SolveDynamic()
{
int j = 0;
foreach (object element in _elementDataCollection)
{
((Thread)_threadArray[j]).Start(element);
j++;
}
}
Is there any way to do it? Or is there any way to do it better?)
PS I've never encountered with Threading in .Net, and I have a small practice in threading programming