Atomics.wait()

静态方法 Atomics.wait() 确保了一个在 Int32Array 数组中给定位置的值没有发生变化、仍然是给定的值时进程将会睡眠,直到被唤醒或超时。该方法返回一个字符串,值为"ok", "not-equal", 或 "timed-out" 之一。

注意: 这项操作仅允许同一个共享内存的 Int32Array 配合使用并且无法运行在主线程中。

语法

Atomics.wait(typedArray, index, value[, timeout])

参数

typedArray
一个共享内存的  Int32Array 数组。
index
给定需要检测的  typedArray 数组的位置索引。
value
给定需要检测的位置索引的预期值。
timeout 可选
超时前等待的毫秒数。  Infinity, 如未提供该参数,将为无穷大。

返回值

一个 String 字符串,值为 "ok", "not-equal", 或 "timed-out" 三种之一。

异常

  • 如果参数 typedArray 不是一个共享内存的 TypeError
  • 如果参数 index 超出了参数 typedArray的边界,将会抛出一个 RangeError

示例

创建一个共享内存的 Int32Array :

var sab = new SharedArrayBuffer(1024);
var int32 = new Int32Array(sab);

检测给定的数组索引0的值,如果它如预期一般的等于我们给定的值0,则这个读取线程将会睡眠等待。一旦当有一个写入线程在这个位置存储了一个新值,它将会收到写入线程的通知并且返回新值 (123) :

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

一旦某个写入线程存储了一个新值到int32 的索引0位置,则通知给该等待线程:

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

规范

规范 状态 备注
ECMAScript Latest Draft (ECMA-262)
Atomics.wait
Draft Initial definition in ES2017.

浏览器支持

Update compatibility data on GitHub
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
wait Chrome Full support 68
Full support 68
No support 60 — 63
Notes
Notes Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This was a temporary removal while mitigations were put in place.
Edge No support 16 — 17
Notes
No support 16 — 17
Notes
Notes Support was removed to mitigate speculative execution side-channel attacks (Windows blog).
Firefox Full support 57
Notes Disabled
Full support 57
Notes Disabled
Notes Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog).
Disabled From version 57: this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No support 55 — 57
No support 48 — 55
Disabled
Disabled From version 48 until version 55 (exclusive): this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No support 46 — 48
Notes Alternate Name Disabled
Notes The method returns values Atomics.OK, Atomics.TIMEDOUT, and Atomics.NOTEQUAL, instead of the later-specified strings.
Alternate Name Uses the non-standard name: futexWait
Disabled From version 46 until version 48 (exclusive): this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support No Opera No support No Safari No support 10.1 — ? WebView Android No support 60 — 63
Notes
No support 60 — 63
Notes
Notes Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.
Chrome Android No support 60 — 63
Notes
No support 60 — 63
Notes
Notes Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.
Firefox Android Full support 57
Notes Disabled
Full support 57
Notes Disabled
Notes Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog).
Disabled From version 57: this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No support 55 — 57
No support 48 — 55
Disabled
Disabled From version 48 until version 55 (exclusive): this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No support 46 — 48
Notes Alternate Name Disabled
Notes The method returns values Atomics.OK, Atomics.TIMEDOUT, and Atomics.NOTEQUAL, instead of the later-specified strings.
Alternate Name Uses the non-standard name: futexWait
Disabled From version 46 until version 48 (exclusive): this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android No support No Safari iOS No support No Samsung Internet Android No support No
Notes
No support No
Notes
Notes Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.
nodejs Full support 8.10.0

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.
Uses a non-standard name.
Uses a non-standard name.

相关参阅