on C# Net 2.0
Code works slightly slower then single threaded version. xDiff = 2100, yDiff = 2000; Almost 14 sec in single threaded, 16 sec in multithreaded (this code). Something must be wrong. I need to fill result array. Only one time writes data on node of array, no reads so it should be appropriate for multi threading.
double[,] result = new double[xDiff, yDiff];
int threadCount = Environment.ProcessorCount;
ManualResetEvent finished = new ManualResetEvent(false);
int perthread = xDiff / threadCount;
int left = xDiff % threadCount;
int toProcess = threadCount;
int s = 0;
int e = left;
for (int ii = 0; ii < threadCount; ii++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object state)
{
for (int x = s; x < e; x++)
for (int y = 0; y < yDiff; y++)
{
result[x, y] = DoWork((xStart + x), (yStart + y), p)
}
if (System.Threading.Interlocked.Decrement(ref toProcess) == 0) finished.Set();
}), null);
s = e;
e += perthread;
}
finished.WaitOne();
return result;
xStart, yStart is double and p is a big class. DoWork function Only calls some functions of p but not writes/change any data on class.
Briefly result[x, y] = DoWork((xStart + x), (yStart + y), p); I need to fill array as fast as i can. How can i do it?