Using Atomics to coordinate the use of shared memory
In the previous recipe, we used multiple workers to produce portions of a result. We could merge these results in the main thread. While valid, this approach doesn't take full advantage of parallel processing. It would be preferable if the workers could accumulate the results themselves.
Mutating shared memory in multiple parallel threads exposes the possibility of race conditions. This is when several operations need to occur in a specific order that is not enforced. Luckily, we can use the Atomics API to coordinate these operations.
In this recipe, we'll see how to use the Atomics API to accumulate results while avoiding race conditions.
Getting ready
This recipe assumes you already have a workspace that allows you to create and run ES modules in your browser. If you don't, please see the first two chapters.
How to do it...
- Open your command-line application and navigate to your workspace.
- Create a new folder named
05-08-use-atomics-to-coordinate...