Atomics Tutorial
Selected Reading
- Atomics - Discussion
- Atomics - Useful Resources
- Atomics - Quick Guide
- Atomics - xor
- Atomics - sub
- Atomics - store
- Atomics - or
- Atomics - notify
- Atomics - load
- Atomics - isLockFree
- Atomics - exchange
- Atomics - compareExchange
- Atomics - and
- Atomics - add
- Atomics - Overview
- Atomics - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Atomics - add
Atomics - add() Method
add method adds a provided value at a given position in the array. It returns the old value at that position. This atomic operation ensures that no other write can happen until the modified value is written back.
Syntax
Atomics.add(typedArray, index, value)
Parameters
typedArray is the integer typed array.
index is position in typedarray.
value to be added.
Return
Returns old value at given position.
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 sopd black; } </style> </head> <body onLoad="operate();"> <h1>JavaScript Atomics Properties</h1> <span class="result"></span> <p>Atomics.add(arr, 0, 2)</p> <p>Atomics.load(arr, 0)</p> <script> function operate(){ let container = document.querySelector(".result"); // create a SharedArrayBuffer var buffer = new SharedArrayBuffer(25); var arr = new Uint8Array(buffer); // Initiapse element at zeroth position of array with 6 arr[0] = 6; container.innerHTML = Atomics.add(arr, 0, 2) + <br/> + Atomics.load(arr, 0); } </script> </body> </html>
Output
Verify the result.
Advertisements