Imagine you have plants and herbivore animals.
Lets take into consideration group of 5 animals close to each other.
Each searches for plants in reach.
It can happen, that multiple animals find same plant at the same time.
If animal eat plant, plants available resources decreases.
So for example, there may be enough food resources for 2 animals, but not for 3 or more.
All plays nicely, when I do such mechanics in single threaded job. Which is fine.
Now lets say, I run animals in IJobChunk.
By my understanding is that, if parallel chunks are running, it may happen that animal from each chunk pick same plant at the same time. So that of course is the problem, as I can run into race condition, when number of animals eat more, than plant can supply.
The way I can think of, is using multi hash map in parallel to store information, which animal tried eat which plant. At this point, no actual resources consumption would occur, just checks and pairing. Need to indicate, not every animal will eat plant at the same frame. Not sure, if that is right application for multi hash maps in parallel.
And even then, I would need iterate through multi hash map elements in next single threaded job, to check and consume resources, if available.
Another problems is, that as far I am aware, cleaning hashamps is not as performant, or I need allocate memory for hashamp. I would need do that every time, I want to run system.
Any other suggestions?