JavaScript | Atomics.notify() Method

Among the Atomic Operations, there is an inbuilt operation Atomics.notify() in JavaScript that is used to notify up some agents that are sleeping in the wait queue. The Atomics.notify() method returns a number of woken up agents. The integer typed array, index, and the count are passed as an argument to the function.

Syntax:

Atomics.notify(typedArray, index, count)

Parameters: This method accept three parameters as mentioned above and described below:

  • typedarray: This parameter specifies a shared integer typed array Int16Array.
  • index: This parameter specifies the position in the array, typedArray to wait on.
  • count: This parameter counts the number of sleeping agents to notify. Its default value is +Infinity.

Return value: The Atomics.notify() method returns the number of woken up agents.

Below programs illustrate the Atomics.notify() method in JavaScript:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

const sab = new SharedArrayBuffer(1024);
const int32 = new Int32Array(sab);
int32[0] = 77; 
console.log(int32[0]);
console.log(Atomics.store(int32, 0, 123)); 
console.log(Atomics.notify(int32, 0, 1)); 

chevron_right


Output:

77
123
0

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

const sab = new SharedArrayBuffer(1024);
const int32 = new Int32Array(sab);
int32[0] = 77; 
console.log(Atomics.load(int32, 66));
console.log(int32[0]);
console.log(Atomics.store(int32, 0, 123)); 
console.log(Atomics.notify(int32, 6, 1));
console.log(int32[0]);

chevron_right


Output:

0
77
123
0
123

Exceptions:

  • If the typedArray is not a shared Int32Array then the Atomics.notify( ) operation throws a TypeError.
  • If the index used as an argument to the Atomics.notify() operation is out of the bound in the typedArray then the Atomics.store( ) operation throws a RangeError.

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.