Been looking for resources and strategies to handle some basic race conditions, but am finding it difficult to find much of anything, or it’s dated. Hoping that the forums will offer some help.
Basic explanation of system: fluid sim using cellular automata methods in a world grid.
So I have three specific types of basic race conditions in a system:
-
Bool Check, if previous value or current value is true, set to true. Basically, I’m trying to check if a physics object is “inFluid”, just a bool check to see if the world grid entities overlapping the object contain fluid. The race condition occurs when two threads are checking two different world grid entities at the same time, one is in fluid and one is not, but if the threads write the new values at the same time, it could end up still coming up false. I’m less concerned about this one, as a race condition is extremely rare and I have a few theories on other thread safe ways I could check if something is in fluid.
-
Iterative addition, value += newValue. Basically I need to transfer fluid from a given entity to another to allow physics objects to “push” and interact with fluid. The race condition occurs when multiple threads are trying to transfer fluid from different source blocks to the same destination block, at the same time.
-
byte | operation, byte |= value. In order to allow physics objects to interact with the fluid sim I’m using a byte to contain whether flow is allowed to pass through the sides of a world grid fluid block (8 if top flow is blocked, 4 for left, 2 for right, and 1 for bottom). The race condition occurs when multiple threads are trying to perform a | byte operation to flip one of the values to on. I’m getting cases where a small amount of fluid will leak through physics objects where it shouldn’t be. This is due to two threads trying to each flip different byte values from 0 to a value at the same time and only one actually getting flipped, so instead of a block having fluid flow of both the top and right sides blocked, it only has the top blocked.
I’ve tried implementing nativequeues to store and track each of these operations and then perform them on a single thread in a threadsafe way (not in parallel) and it fixed all the bugs, but it came at a higher cost than I would like, so I’m hoping I can find some way of doing these operations in parallel in a thread safe way. I’m hoping that there is some new API methods or some preferred strategies that people are currently using to handle these sorts of race conditions, but I haven’t been able to find anything clear/recent on the google besides some vague suggestions, most of the stuff that comes up is basically saying to just not have race conditions in the first place, but these iterative operations are basically required for my fluid sim to work, not sure how else I could do it.