Atomics.notify()
Atomics.notify() 静态方法通知一些在等待队列中休眠的代理。
¥The Atomics.notify() static
method notifies up some agents that are sleeping in the wait queue.
注意:此操作仅适用于查看
SharedArrayBuffer的Int32Array或BigInt64Array。它将在非共享ArrayBuffer对象上返回0。¥Note: This operation only works with an
Int32ArrayorBigInt64Arraythat views aSharedArrayBuffer. It will return0on non-sharedArrayBufferobjects.
语法
参数
¥Parameters
typedArray-
查看
SharedArrayBuffer的Int32Array或BigInt64Array。 index-
typedArray中唤醒的位置。 countOptional-
要通知的休眠代理的数量。默认为
Infinity。
返回值
例外情况
¥Exceptions
TypeError-
如果
typedArray不是查看SharedArrayBuffer的Int32Array或BigInt64Array,则抛出该异常。 RangeError-
如果
index在typedArray中出界,则抛出该球。
示例
使用 notify
¥Using notify
给定一个共享的 Int32Array:
¥Given a shared Int32Array:
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).
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:
console.log(int32[0]); // 0;
Atomics.store(int32, 0, 123);
Atomics.notify(int32, 0, 1);
规范
| Specification |
|---|
| ECMAScript Language Specification # sec-atomics.notify |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also