Atomics.isLockFree( ) In JavaScript

What is Atomics?

  • The Atomics is an object in JavaScript which provides the ability to perform atomic operations as static methods.
  • Just like the Math object in JavaScript all the properties and methods of Atomics are also static.
  • Atomics are used with SharedArrayBuffer(generic fixed-length binary data buffer) objects.
  • Atomics are not constructors like other global objects.
  • Atomics cannot be used with a new operator or can be invoked as a function.

Atomics Operations in JavaScript
Multiple threads can read and write the same data in memory when there shared memory is. To ensure that predicted values are written and read accurately, another operation cannot start until and unless the current one finishes. Atomic operations also cannot be interrupted.

Atomics.isLockFree() Method



    Among the Atomic Operations, there is an inbuilt operation Atomics.isLockFree() that is used to determine whether to use locks or atomic operations.

  • Atomics.isLockFree() operation returns true if the given size is one of the BYTES_PER_ELEMENT property of integer TypedArray types else Atomics.isLockFree() operation returns false.
  • A lock free element can be manipulated without needing a lock and the user does not require to provide its own locking mechanism.

What is BYTES_PER_ELEMENT property of integer TypedArray ?

  • The TypedArray.BYTES_PER_ELEMENT property represents the size in bytes of each element in a typed array.
  • Since TypedArray objects differ from each other in the number of bytes per element and in the way the bytes are interpreted.
  • The BYTES_PER_ELEMENT constant contains the number of bytes each element in the given TypedArray has.

Applications:

  • Atomics.isLockFree() are used to check whether an operation is lock free or not.
  • Can be used for the validation of the BYTES_PER_ELEMENT property of integer TypedArray.

Syntax:

Atomics.isLockFree(size)

Parameters Used:
size : It is the size in bytes to check

Return Value:
Atomics.isLockFree() returns either Boolean true indicating the operation is lock free or it returns false.

Examples of the above function are provided below.

Input : Atomics.isLockFree(5)
Output : false

Explanation: In this example, “5” is sent as a parameter to the Atomics.isLockFree() method and it returns false because “5” is not one of the BYTES_PER_ELEMENT values.

Input : Atomics.isLockFree(6)
Output : false

Explanation: In this example, “6” is sent as a parameter to the Atomics.isLockFree() method and it returns false because “6” is not one of the BYTES_PER_ELEMENT values.

Input : Atomics.isLockFree(2)
Output : true

Explanation: In this example, “2” is sent as a parameter to the Atomics.isLockFree() method and it returns true because “2” is one of the BYTES_PER_ELEMENT values.



Input : Atomics.isLockFree(4)
Output : true

Explanation: In this example, “4” is sent as a parameter to the Atomics.isLockFree() method and it returns true because “4” is one of the BYTES_PER_ELEMENT values.
Codes for the above function are provided below.

Code 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// Displaying the return value of the 
// Atomics.isLockFree() method 
console.log(Atomics.isLockFree(5));
  
// Atomics.isLockFree() will return false since
// 5 is not one of the BYTES_PER_ELEMENT values 
</script>

chevron_right


OUTPUT :

false

Code 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// Displaying the return value of
// the Atomics.isLockFree() method 
console.log(Atomics.isLockFree(6));
  
// Atomics.isLockFree() will return false since 6 
// is not one of the BYTES_PER_ELEMENT values 
</script>

chevron_right


OUTPUT :

false

Code 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// Displaying the return value
// of the Atomics.isLockFree() method 
console.log(Atomics.isLockFree(2));
  
// Atomics.isLockFree() will return true since 
// 2 is one of the BYTES_PER_ELEMENT values 
</script>

chevron_right


OUTPUT :

true

Code 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// Displaying the return value of the
// Atomics.isLockFree() method 
console.log(Atomics.isLockFree(4));
  
// Atomics.isLockFree() will return true since
//  4 is one of the BYTES_PER_ELEMENT values 
</script>

chevron_right


OUTPUT :

true

Application:
Whenever we want to check whether an operation is lock-free or not or we want to validate the BYTES_PER_ELEMENT property of integer TypedArray, we use the Atomics.isLockFree() operation in JavaScript.

Let’s see a JavaScript Program :

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// Displaying the return value of 
// the Atomics.isLockFree() method 
console.log(Atomics.isLockFree(8));
  
// Atomics.isLockFree() will return true since 8 
// is one of the BYTES_PER_ELEMENT(Float64Array) values
</script>

chevron_right


Output :

true

Exceptions :