I’ve been trying to parallelize the following function but cannot figure out how to.
public static Cell GetClosestCell (Cell cell)
{
// The four calls below should be run in parallel.
Cell temp1 = new FindNorth(cell);
Cell temp2 = new FindSouth(cell);
Cell temp3 = new FindWest(cell);
Cell temp4 = new FindEast(cell);
// Return smallest cell based on [X].
if ((temp1.X < temp2.X) && (temp1.X < temp3.X) && (temp1.X < temp4.X))
{
return (temp1);
}
else if ((temp2.X < temp3.X) && (temp2.X < temp4.X))
{
return (temp2);
}
else if (temp3.X < temp4.X)
{
return (temp3);
}
else
{
return (temp4);
}
}
Each of the four function calls should run in parallel but without having to initiate a thread. In other words, there should be 4 threads already running waiting for input to which I can dispatch each call.
I’m used to the normal paradigm of parallel loops and not sure how to approach this (at least not in a clean way).
1 Answer