Background: I hope my question is not too vague. I will try and explain as much as possible, and cannot post too much code, as the affected method is very complex and lengthy. So my problem is that I am trying to speed up processing with multiple threads. The application is for lighting in my 2D game engine, where I draw black rectangles of different transparency levels on top om my scene (which is currently causing lag).
The first step for me was to batch adjacent rectangles of equal light level together to reduce some rendering work. This worked out fine (perhaps it can be done better, but not the point now), and now I also implemented a crude threading system, which will each batch together lighting for a separate portion of the screen.
Crux of my question: While analyzing the timing of the particular batching method described above (for different number of threads working on separate, disjoint data sets), I noticed some odd spikes. Previously, when working with one thread, the method took about 12ms to execute, with odd jumps to 15ms. I didn't think much of it. When processing with 2 or 3 threads however, I get more or less 4~5ms, with jumps to 10ms, and sometimes even as high as 20ms.
Now I realize that it is not possible for anyone to tell what the cause could be without inspecting my code, so I do not expect that. Rather, I am trying to draw some conclusions and wish to confirm them now. As stated before, each thread does work on portions of my data set, completely disjoint from one another (they do not overlap). However, the entry point to the data array is through the same method of the same instance of a particular class. So the threads all access the same array (yet separate portions of it - and for this reason I also don't use any locks) through the same method. Could this cause unexpected slow downs?
Or is it perhaps normal behavior for threads to act in this way (with execution time varying to more than double of the norm)? As another note, I create all threads at the moment I need them, and let them run to completion.
The accessing method for the data set:
public short GetLightLevelAt(int x, int y)
{
if (inLightingBounds(x, y))
{
return lightData[x, y];
}
else
{
return GuessLightLevelAt(x,y); //This won't ever happen currently, guaranteed
}
}
locksomething, and some other thread tries to access it. The second thread must wait for the first thread to release its lock.