What's the actual use of the Atomics object in ECMAScript?

后端 未结 5 1113
Happy的楠姐
Happy的楠姐 2021-02-07 23:04

The ECMAScript specification defines the Atomics object in the section 24.4.

Among all the global objects this is the more obscure for

5条回答
  •  遇见更好的自我
    2021-02-07 23:29

    I have coded a script using Web Worker and SharedArrayBuffer to demonstrate the use of Atomics:

    
    

    Then with a separate file worker.js:

    // worker.js
    onmessage = function(e) {
        e.data[0]++;                 // last line is 981 only? wth?!
        //Atomics.add(e.data,0,1);   // last line is exactly 1000. right...
        console.log(e.data[0]);
    }
    

    As you can see, without the mutex guaranteed by Atomics, the addition would not be correctly carried out sometimes.

提交回复
热议问题