Atomics - notify() Method


Advertisements

notify method notifies the waiting agent to wake up. notify method can work only with Int32Array created using SharedArrayBuffer. It returns 0 in case of non-shared ArrayBuffer object is used.

Syntax

Atomics.notify(typedArray, index, count)

Parameters

  • typedArray is a shared Int32Array.

  • index is position in typedarray to wake up on.

  • count is the number of sleeping agents to notify.

Return

Returns the number of agents woken up.

Exceptions

  • TypeError in case passed array is not a integer typed array.

  • RangeError if index passed is out of bound in typed array.

Example

Following is the code for implementing JavaScript Atomics −

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Atomics Example</title>
      <style>
         .result {
            font-size: 20px;
            border: 1px solid black;
         }
      </style>
   </head>
   <body onLoad="operate();">
      <h1>JavaScript Atomics Properties</h1>
      <div class="result"></div>
      <p>Atomics.store(arr, 0, 5)</p>
      <p>Atomics.notify(arr, 0, 1)</p>
      <script>
         function operate(){
            let container = document.querySelector(".result");
            // create a SharedArrayBuffer
            var buffer = new SharedArrayBuffer(16);
            var arr = new Int32Array(buffer);
            // Initialise element at zeroth position of array with 6
            arr[0] = 6;            
            container.innerHTML = Atomics.store(arr, 0, 5) + '<br>' + Atomics.notify(arr, 0, 1);
         }
      </script>
   </body>
</html>

Output

Verify the result.

Advertisements