Handling with threads could be simplified, i.E. take this example from C# which uses the Parallel Extions Library from MS and ist quite similar to Threading Building Blocks from Intel:
The secret lies in the library, the library knows:
- how many cores are availaible
- which granularity to use
It creates itself the ideal amount of threads an speeds up near to linearity. To use parallelis in constructs, where it could easily done - is much more error safe and easier to develop like an "real" threaded game where you have an extra renderthread, ai-threa, physics-thread, io-thread....
Parallel.For(0, height, j =>
{
PixelData* outPixel = fastOut.GetInitialPixelForRow(j);
PixelData* startPixel = fastStart.GetInitialPixelForRow(j);
PixelData* endPixel = fastEnd.GetInitialPixelForRow(j);
for (int i = 0; i < width; i++)
{
// Blend the input pixels into the output pixel
outPixel->R = (byte)((startPixel->R * blend) + .5 + (endPixel->R * (1 - blend))); // .5 for rounding
outPixel->G = (byte)((startPixel->G * blend) + .5 + (endPixel->G * (1 - blend)));
outPixel->B = (byte)((startPixel->B * blend) + .5 + (endPixel->B * (1 - blend)));
outPixel++;
startPixel++;
endPixel++;
}
});
The secret lies in the library, the library knows:
- how many cores are availaible
- which granularity to use
It creates itself the ideal amount of threads an speeds up near to linearity. To use parallelis in constructs, where it could easily done - is much more error safe and easier to develop like an "real" threaded game where you have an extra renderthread, ai-threa, physics-thread, io-thread....