I’m new to threading and parallelism. I have this method for a game in C# and need to use parallel iteration. How could I use this on the for loops in the below method?
public int[,] GetLegalMoves()
{
int[,] legalMoves = new int[8, 8];
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
if (IsMoveLegal(i, j)) legalMoves[i, j] = 1;
else legalMoves[i, j] = 0;
return legalMoves;
}
This could be parallelized by parallelizing your outer loop:
That being said, this will likely cause this to run slower, as an 8×8 matrix is such a small value that the overhead of scheduling the parallel work is likely higher than the gains made, unless
IsMoveLegalis a fairly expensive operation.This will also require that
IsMoveLegalbe safe to use from multiple threads.