I have a method named render
that’s called on every frame to draw something on screen using Metal. It is rendering some array of points stored inside the class. However, I occasionally need to update this array of points. I’m struggling to come up with a solution that could update these points in a concurrency safe manner.
Namely, when render
is attempting to access this array, another thread could be updating it, leading to a crash. Additionally, render
is synchronous and cannot block on or await the update. This seems like a commonly occurring concurrency problem so I’m seeking help here to see what solutions there are.
To generalize the problem — there’s some synchronous function that’s called very frequently, it needs access to a piece of data that could be updated by another thread. I do have control over both the render
method and the “other” thread that does the update. How can I prevent a race condition from crashing the app? (it just has to not crash, there’s no data “correctness” considerations)
One possible solution I hypothesized is using atomics I’m not exactly sure how it could be done, especially in Swift (don’t think Swift has atomics?)