This is the MDN documentation on Atomics.
I can't understand how an atomic object could be used in a real-life situation or if they are for internal use only (i.e. not meant to be called at all).
What does the Atomics
object do in JavaScript? How can I use them?
The Atomics is an object in JavaScript which provides atomic operations to be performed as static methods. Just like the methods of Math object, the methods and properties of Atomics are also static. Atomics are used with SharedArrayBuffer objects. The Atromic operations are installed on an Atomics Module.
By an "atomic object" we understand an object whose public interface exposes only atomic operations, i.e. all operations you can do with that object are atomic.
Atomicity. In computer programming, an operation done by a computer is considered atomic if it is guaranteed to be isolated from other operations that may be happening at the same time.
Atomic operations are sequences of instructions that guarantee atomic accesses and updates of shared single word variables. This means that atomic operations cannot protect accesses to complex data structures in the way that locks can, but they provide a very efficient way of serializing access to a single word.
This feature was introduced with ECMAScript 2017. Shared Memory
and Atomics
introduce a new memory model that allows multi-agent programs to communicate using atomic operations that ensure a well-defined execution order even on parallel CPUs. This specification also includes new static methods on Object
: Object.values
, Object.entries
, and Object.getOwnPropertyDescriptors
.
The main idea is to bring some sort of multi-threading feature to JavaScript so that JS developers can write high-performance, concurrent programs in the future by allowing to manage memory by themselves instead of letting JS engine manage memory.
This is done by a new type of a global object called SharedArrayBuffer
that essentially stores data in a shared memory space. So this data can be shared between the main JS thread and web-worker threads.
You simply use SharedArrayBuffer
and the data is instantly accessible by both the main thread and multiple web-worker threads.
But sharing memory between threads can cause race conditions. To help avoid race conditions, the Atomics
global object is introduced. Atomics
provides various methods to lock the shared memory when a thread is using its data. It also provides methods to update such data in that shared memory safely.
Let's wait some new abstract library which will use those API's right.
shared memory and atomics
a cartoon intro to SharedArrayBuffers
JavaScript: From Workers to Shared Memory
ECMAScript 2019. Memory Model
Avoiding race conditions in SharedArrayBuffers with Atomics
Also recommend to see the tutorial about shared memory in ECMAScript.
Dangers of multithreaded programming
To solve the previous problem what can we do?
Use the atomic option
The Atomics object provides atomic operations such as add, sub, and, or, xor, load, store etc. as static methods. They are used with SharedArrayBuffer objects.
For more information and a good example: Source:https://www.slideshare.net/barakdrechsler/atomic-javascript
https://jirak.net/wp/introducing-new-javascript-optimizations-webassembly-sharedarraybuffer-and-atomics-in-edgehtml-16/
https://www.geeksforgeeks.org/atomics-or-in-javascript/
For good resource chapter 12 SharedArrayBuffer in:
Phang, C. L. (2017). An Effective Guide to Modern JavaScript:(ECMAScript 2017/ES 8). Chong Lip Phang.
<iframe frameborder="0" scrolling="no" style="border:0px" src="https://books.google.com.tw/books?id=--gvDwAAQBAJ&lpg=PP3&ots=zqgshUnSQa&dq=atomic%20%20SharedArrayBuffer%20%20for%20javascript&lr&pg=PA80&output=embed" width=500 height=500></iframe>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With