(ES6) ES6 (2016)

Atomic (AT01)

   1:  const sab = new SharedArrayBuffer(1024); // Create shared memory
   2:  const int32Array = new Int32Array(sab); // Create Typed Array view
   3:  
   4:  // Different workers or threads can now access int32Array.
   5:  
   6:  // In worker 1:
   7:  Atomics.add(int32Array, 0, 5); // Atomically increment the first element by 5
   8:  
   9:  
  10:  // In worker 2:
  11:  const value = Atomics.load(int32Array, 0); // Atomically read the value
  12:  console.log("Value from worker 2:", value);



The Atomics object provides a variety of methods for different atomic operations. Here are some of the most commonly used ones:
    • Atomics.load(typedArray, index): Reads the value at the given index from the typedArray atomically. Returns the value.
    • Atomics.store(typedArray, index, value): Writes the value to the given index in the typedArray atomically. Returns the value that was stored.
    • Atomics.add(typedArray, index, value): Atomically adds value to the element at index in typedArray. Returns the original value before the addition.
    • Atomics.sub(typedArray, index, value): Atomically subtracts value from the element at index in typedArray. Returns the original value before the subtraction.
    • Atomics.and(typedArray, index, value): Atomically performs a bitwise AND operation between the element at index and value. Returns the original value.
    • Atomics.or(typedArray, index, value): Atomically performs a bitwise OR operation. Returns the original value.
    • Atomics.xor(typedArray, index, value): Atomically performs a bitwise XOR operation. Returns the original value.
    • Atomics.exchange(typedArray, index, value): Atomically swaps the value at index with value. Returns the original value.
    • Atomics.compareExchange(typedArray, index, expectedValue, replacementValue): If the current value at index equals expectedValue, atomically sets it to replacementValue. Returns the original value. This is useful for implementing lock-free algorithms.
    • Atomics.wait() and Atomics.notify() (for synchronization): These methods are used for synchronization between agents (like web workers). Atomics.wait allows a thread to wait for a change on shared memory. Atomics.notify() can be used to wake up waiting threads.
    • Atomics.waitAsync(): This method allows asynchronous waiting on shared memory locations. It's a more efficient alternative to repeatedly polling with Atomics.wait() and helps prevent busy-waiting (which consumes excessive CPU resources). It's particularly useful when coordinating tasks between different workers.
    • Atomics.notify() Improvements: While Atomics.notify() existed before ES2022, changes in ES2022 provide more control over the notification behavior. Before, it would wake up all agents waiting on the same memory location, potentially leading to unnecessary wake-ups or contention. The improved Atomics.notify() allows you to specify the maximum number of waiting agents to wake up. This optimization helps in scenarios with many threads collaborating on shared memory.





Atomic context:






ES6 context:



Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23>  <24>  <25
Link to this page: http://www.vb-net.com/JavascriptES6/AT01.htm
<TAGS>  <ARTICLES>  <FRONT>  <CORE>  <MVC>  <ASP>  <NET>  <DATA>  <TASK>  <XML>  <KIOSK>  <NOTES>  <SQL>  <LINUX>  <MONO>  <FREEWARE>  <DOCS> <TRAVELS> <FLOWERS> <RESUME> < THANKS ME>