Indeed. And there’s a whole Atomics namespace in the standard library for thread safety while using it.
Again, there’s very few actual uses for wasm. The main one is when you have a preexisting library in a different language, other than that you don’t gain any real functionality from using it.
SharedArrayBuffers are helpful for simple data types but because they are a simple fixed size array of bytes, you cannot share things like objects, class instances, arrays, etc and you need to manually extend the SAB by creating multiple SAB "pages" and manually glue them together. Ultimately it's not useful for most practical use-cases.
In the past I have used a SharedArrayBuffer to store a custom Graph implementation sent between threads. I stored the adjacency list in the SAB with the rest of the data being stored as a plain object and cloned/sent via postMessage between threads.
This worked but is extremely janky, required an unsafe custom data structure, manual/unsafe memory page management, was difficult to read/review and the performance was lacklustre because half of the data structure was plain JS objects that needed to be serialized and cloned anyway.
Simply, JavaScript sucks for multithreading and it would be great to have the ability to write high performance, multithreaded, complex web applications in a language designed with parallelism in mind - like Rust or Go.
While wasm _today_ cannot offer that (at least not practically) - there is certainly a use case for replacing JavaScript in that context and I hope that it does.
JS will still be useful for basic applications that just need a little scripting
SAB solves the problem of having a small number of dedicated workers that efficiently handle specific well constrained data processing tasks.
I must question whether the problem of random sites wanting to spin up a whole ton of cores that consume arbitrary amounts of compute and memory really needs to be solved.
Again, there’s very few actual uses for wasm. The main one is when you have a preexisting library in a different language, other than that you don’t gain any real functionality from using it.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...