Hello I am ne to multithreading and trying to incorporate it into my program in order to get a particular calculation to converge faster. But when I create the two threads like below
Thread firstThread = new Thread(() => { p0 = f(changePoint, result); });
Thread secondThread = new Thread(() => { p1 = f(changePoint + 1, result); });
firstThread.Start();
secondThread.Start();
firstThread.Join();
secondThread.Join();
the values in p0 and p1 are the same(p0 has the same value as p1). Now if i take away the multithreading and just call them like this:
p0 = f(changePoint, result);
p1 = f(changePoint + 1, result);
different values are returned and everything works correctly.
What am i missing?
code of f(x,y)
public double f(double x,double result)
{
double temp = PCAcont.Future2Yrs(x).Last().FirstOrDefault().StatNetWorthToAssets.GetValueOrDefault();
return temp - result;
}
the main method
public double SecantMethod(double prec, int stepsCutoff, double changePoint, double result)
{
double p2, p1 = 0, p0 = 0;
int i;
Thread firstThread = new Thread(() => { p0 = f(changePoint, result); });
Thread secondThread = new Thread(() => { p1 = f(changePoint + 1, result); });
firstThread.Start();
secondThread.Start();
firstThread.Join();
secondThread.Join();
//p0 = f(changePoint, result);
//p1 = f(changePoint + 1, result);
p2 = p1 - f(p1, result) * (p1 - p0) / (f(p1, result) - f(p0, result));
for (i = 0; System.Math.Abs(p2 - p1) > prec && i < stepsCutoff; i++)
{
p0 = p1;
p1 = p2;
p2 = p1 - f(p1, result) * (p1 - p0) / (f(p1, result) - f(p0,result));
}
if (i < stepsCutoff)
return p2;
else
{
System.Diagnostics.Debug.WriteLine("{0}.The method did not converge", p2);
return double.NaN;
}
}