help icon indicating copy to clipboard operation
help copied to clipboard

With a SharedArrayBuffer, is TypedArray.set([...values],index) thread-safe from the point of view of reading threads?

Open nerdgod opened this issue 1 year ago • 3 comments

Node.js Version

v22.9.0

NPM Version

v10.8.3

Operating System

Windows

Subsystem

worker_threads

Description

Let's say I have a SharedArrayBuffer sab, and various threads have access to it, but only one thread writes to it.

Reader threads map sab to a Uint8Array arr, and they call Atomics.wait(arr,0,0), and wait for the writer to set that flag, before reading from arr[1] onward.

The writer, using its own Uint8Array arr0 mapped to sab, calls

arr0.set([...possibly-many-pages-of-data],1);
Atomics.store(arr0,0,1);
Atomics.notify(arr0,0)

The question is: does Atomics.store() then ensure that arr0.set is a complete write and all threads reading from ANY part of sab (regardless of which portion a given TypedArray may be mapped to, upon which Atomics.notify() was called) have synchronized data once their wait returns ok?

Minimal Reproduction

No response

Output

No response

Before You Submit

  • [X] I have looked for issues that already exist before submitting this
  • [X] My issue follows the guidelines in the README file, and follows the 'How to ask a good question' guide at https://stackoverflow.com/help/how-to-ask

nerdgod avatar Oct 11 '24 17:10 nerdgod