- Home /
How to multithread a large sequence
I'm working on a 2D top-down game and, to simulate our water (and various other environmental hazards which have direct gameplay relevance), I use a large 2D grid that stores what hazard is in a given space. At an interval, I iterate through the grid and each space reacts to its 4 adjacent spaces (before, after, above, and below). This process isn't problematically slow, but I dislike how it is all bound to one thread and takes up a good chunk of time whenever there's a large amount of hazards in the simulated area.
Problem is: I have no clue how to multi-thread a large amount of small calculations that directly rely on the state of adjacent data, nor do I know how to phrase this question to even google a good article on the subject.
Answer by noonoox · Mar 28, 2020 at 06:15 AM
The method I ended up using to solve my problem was to break my large 2D array into chunks (for example, let's say 10) and ran the calculations for each chunk on a different thread, but left the edge-pieces of data (that would affect / be affected by the adjacent pieces in other chunks) alone. Upon finishing all chunks, I'd then iterate through the skipped parts on the main thread (or, in Unity's case, a single job with burst compilation for extra lovely speed) to form a cohesive whole.
Your answer
![](https://koobas.hobune.stream/wayback/20220612225017im_/https://answers.unity.com/themes/thub/images/avi.jpg)