Skip to content

Instantly share code, notes, and snippets.

@jackmott
Created January 10, 2019 15:05
Show Gist options
  • Save jackmott/27808f177b933c3fe9967eb268aa5de3 to your computer and use it in GitHub Desktop.
Save jackmott/27808f177b933c3fe9967eb268aa5de3 to your computer and use it in GitHub Desktop.
What could be thread unsafe about this?
// When this function runs in my program on multiple threads, memory corruption eventually happens
// When it doesn't run, or runs on only 1 thread, no problems
// I can't see what could be thread unsafe about it, am I missing something?
public static float SingleCellular2EdgeSimple(float x, float y, float jitter, int seed)
{
int xr = (x >= 0) ? (int)(x + 0.5f) : (int)(x - 0.5f);
int yr = (y >= 0) ? (int)(y + 0.5f) : (int)(y - 0.5f);
float[] distance = { 999999f, 999999f, 999999f, 999999f };
for (int xi = xr - 1; xi <= xr + 1; xi++)
{
for (int yi = yr - 1; yi <= yr + 1; yi++)
{
int hash = seed;
hash ^= 1619 * xi;
hash ^= 31337 * yi;
hash = hash * hash * hash * 60493;
hash = (hash >> 13) ^ hash;
//Float2 vec = new Float2(0.5f, 0.5f); // CELL_2D[hash & 255];
float vecX = xi - x + 0.5f * jitter;
float vecY = yi - y + 0.5f * jitter;
float newDistance = vecX * vecX + vecY * vecY;
for (int i = 1; i > 0; i--)
distance[i] = Math.Max(Math.Min(distance[i], newDistance), distance[i - 1]);
distance[0] = Math.Min(distance[0], newDistance);
}
}
return distance[1] + distance[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment