How to do it...

  1. Open your command-line application, and navigate to your workspace.
  2. Create a new folder named 05-05-creating-shared-array-buffer.
  3. Copy or create an index.html that loads and runs a main function from main.js.
  1. Create a main.js file with a main method that defines constants for NUM_COUNT, BYTES_FOR_32_BIT, ARRAY_SIZE, and MAX_NUMBER:
export function main() { 
  const NUM_COUNT = 2048; 
  const BYTES_FOR_32_BIT = 4; 
  const ARRAY_SIZE = NUM_COUNT * BYTES_FOR_32_BIT; 
  const MAX_NUMBER = 1024; 
} 
  1. Next, create a SharedArrayBuffer that is of size ARRAY_SIZE, and create an Int32Array casting of it:
export function main() { 
  // ... 
  const sab = new SharedArrayBuffer(ARRAY_SIZE);
const intBuffer = new Int32Array(sab); }
  1. Fill the intBuffer with random numbers between 0 and MAX_NUMBER:
export function main() { 
  // ... 
  // fill with random numbers  // fill with random numbers
intBuffer.forEach((value, index) => {
intBuffer[index] = Math.random() * MAX_NUMBER;
})
;
}
  1. Calculate and print the sum of the values in the array:
 export function main() { 
  // ... 
  // sum the ints
const sum = intBuffer.reduce((acc, number) =>
acc + number
, 0);
}
  1. Start your Python web server and open the following link in your browser:
    http://localhost:8000/.
  1. You should see the following output:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.163.50