Created
January 10, 2019 15:05
-
-
Save jackmott/27808f177b933c3fe9967eb268aa5de3 to your computer and use it in GitHub Desktop.
What could be thread unsafe about this?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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