Atomics.notify()

Atomics.notify() 静态方法通知一些在等待队列中休眠的代理。

¥The Atomics.notify() static method notifies up some agents that are sleeping in the wait queue.

注意:此操作仅适用于查看 SharedArrayBufferInt32ArrayBigInt64Array。它将在非共享 ArrayBuffer 对象上返回 0

¥Note: This operation only works with an Int32Array or BigInt64Array that views a SharedArrayBuffer. It will return 0 on non-shared ArrayBuffer objects.

语法

¥Syntax

js
Atomics.notify(typedArray, index, count)

参数

¥Parameters

typedArray

查看 SharedArrayBufferInt32ArrayBigInt64Array

index

typedArray 中唤醒的位置。

count Optional

要通知的休眠代理的数量。默认为 Infinity

返回值

¥Return value

  • 返回已唤醒代理的数量。
  • 如果使用非共享 ArrayBuffer 对象,则返回 0

例外情况

¥Exceptions

TypeError

如果 typedArray 不是查看 SharedArrayBufferInt32ArrayBigInt64Array,则抛出该异常。

RangeError

如果 indextypedArray 中出界,则抛出该球。

示例

¥Examples

使用 notify

¥Using notify

给定一个共享的 Int32Array

¥Given a shared Int32Array:

js
const sab = new SharedArrayBuffer(1024);
const int32 = new Int32Array(sab);

读取线程正在睡眠并等待位置 0,该位置预计为 0。只要这是真的,它就不会继续下去。然而,一旦写入线程存储了新值,它将被写入线程通知并返回新值(123)。

¥A reading thread is sleeping and waiting on location 0 which is expected to be 0. As long as that is true, it will not go on. However, once the writing thread has stored a new value, it will be notified by the writing thread and return the new value (123).

js
Atomics.wait(int32, 0, 0);
console.log(int32[0]); // 123

写入线程存储新值并在写入后通知等待线程:

¥A writing thread stores a new value and notifies the waiting thread once it has written:

js
console.log(int32[0]); // 0;
Atomics.store(int32, 0, 123);
Atomics.notify(int32, 0, 1);

规范

Specification
ECMAScript Language Specification
# sec-atomics.notify

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看