Atomics
对象提供了一组静态方法用来对 SharedArrayBuffer
对象进行原子操作。
这些原子操作属于 Atomics
模块。与一般的全局对象不同,Atomics
不是构造函数,因此不能使用 Math
对象一样)。
属性
-
Atomics[Symbol.toStringTag]
- 该属性的值为“Atomics”。
方法
原子操作
多个共享内存的线程能够同时读写同一位置上的数据。原子操作会确保正在读或写的数据的值是符合预期的,即下一个原子操作一定会在上一个原子操作结束后才会开始,其操作过程不会中断。
-
Atomics.add()
- 将指定位置上的数组元素与给定的值相加,并返回相加前该元素的值。
-
Atomics.and()
- 将指定位置上的数组元素与给定的值相与,并返回与操作前该元素的值。
-
Atomics.compareExchange()
- 如果数组中指定的元素与给定的值相等,则将其更新为新的值,并返回该元素原先的值。
-
Atomics.exchange()
- 将数组中指定的元素更新为给定的值,并返回该元素更新前的值。
-
Atomics.load()
- 返回数组中指定元素的值。
-
Atomics.or()
- 将指定位置上的数组元素与给定的值相或,并返回或操作前该元素的值。
-
Atomics.store()
- 将数组中指定的元素设置为给定的值,并返回 该值。
-
Atomics.sub()
- 将指定位置上的数组元素与给定的值相减,并返回相减前该元素的值。
-
Atomics.xor()
- 将指定位置上的数组元素与给定的值相异或,并返回异或操作前该元素的值。
等待和唤醒
wait()
和 wake()
方法采用的是 Linux 上的 futexes 模型(fast user-space mutex,快速用户空间互斥量),可以让进程一直等待直到某个特定的条件为真,主要用于实现阻塞。
-
Atomics.wait()
-
检测数组中某个指定位置上的值是否仍然是给定值,是则保持挂起直到被唤醒或超时。返回值为 "
ok
"、"not-equal
" 或 "time-out
"。调用时,如果当前线程不允许阻塞,则会抛出异常(大多数浏览器都不允许在主线程中调用wait()
)。 -
Atomics.wake()
- 唤醒等待队列中正在数组指定位置的元素上等待的线程。返回值为成功唤醒的线程数量。
-
Atomics.isLockFree(size)
-
可以用来检测当前系统是否支持硬件级的原子操作。对于指定大小的数组,如果当前系统支持硬件级的原子操作,则返回
true
;否则就意味着对于该数组,Atomics
对象中的各原子操作都只能用锁来实现。此函数面向的是技术专家。
规范
规范 | 状态 | 说明 |
---|---|---|
ECMAScript Latest Draft (ECMA-262) Atomics |
Draft | ES2017 中的初始定义 |
ECMAScript 2017 (ECMA-262) Atomics |
Standard |
浏览器兼容性
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out
https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Atomics |
Chrome Full support 68
|
Edge No support 16 — 17
|
Firefox Full support 57
|
IE No support No | Opera No support No | Safari No support 10.1 — ? | WebView Android No support 60 — 63
|
Chrome Android No support 60 — 63
|
Firefox Android Full support 57
|
Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No
|
nodejs Full support 8.10.0 |
add |
Chrome Full support 68
|
Edge No support 16 — 17
|
Firefox Full support 57
|
IE No support No | Opera No support No | Safari No support 10.1 — ? | WebView Android No support 60 — 63
|
Chrome Android No support 60 — 63
|
Firefox Android Full support 57
|
Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No
|
nodejs Full support 8.10.0 |
and |
Chrome Full support 68
|
Edge No support 16 — 17
|
Firefox Full support 57
|
IE No support No | Opera No support No | Safari No support 10.1 — ? | WebView Android No support 60 — 63
|
Chrome Android No support 60 — 63
|
Firefox Android Full support 57
|
Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No
|
nodejs Full support 8.10.0 |
compareExchange |
Chrome Full support 68
|
Edge No support 16 — 17
|
Firefox Full support 57
|
IE No support No | Opera No support No | Safari No support 10.1 — ? | WebView Android No support 60 — 63
|
Chrome Android No support 60 — 63
|
Firefox Android Full support 57
|
Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No
|
nodejs Full support 8.10.0 |
exchange |
Chrome Full support 68
|
Edge No support 16 — 17
|
Firefox Full support 57
|
IE No support No | Opera No support No | Safari No support 10.1 — ? | WebView Android No support 60 — 63
|
Chrome Android No support 60 — 63
|
Firefox Android Full support 57
|
Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No
|
nodejs Full support 8.10.0 |
isLockFree |
Chrome Full support 68
|
Edge No support 16 — 17
|
Firefox Full support 57
|
IE No support No | Opera No support No | Safari No support 10.1 — ? | WebView Android No support 60 — 63
|
Chrome Android No support 60 — 63
|
Firefox Android Full support 57
|
Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No
|
nodejs Full support 8.10.0 |
load |
Chrome Full support 68
|
Edge No support 16 — 17
|
Firefox Full support 57
|
IE No support No | Opera No support No | Safari No support 10.1 — ? | WebView Android No support 60 — 63
|
Chrome Android No support 60 — 63
|
Firefox Android Full support 57
|
Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No
|
nodejs Full support 8.10.0 |
notify |
Chrome Full support 68
|
Edge No support No
|
Firefox Full support 63
|
IE No support No | Opera No support No | Safari No support 10.1 — ?
|
WebView Android No support 60 — 63
|
Chrome Android No support 60 — 63
|
Firefox Android Full support 63
|
Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No
|
nodejs Full support 8.10.0
|
or |
Chrome Full support 68
|
Edge No support 16 — 17
|
Firefox Full support 57
|
IE No support No | Opera No support No | Safari No support 10.1 — ? | WebView Android No support 60 — 63
|
Chrome Android No support 60 — 63
|
Firefox Android Full support 57
|
Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No
|
nodejs Full support 8.10.0 |
store |
Chrome Full support 68
|
Edge No support 16 — 17
|
Firefox Full support 57
|
IE No support No | Opera No support No | Safari No support 10.1 — ? | WebView Android No support 60 — 63
|
Chrome Android No support 60 — 63
|
Firefox Android Full support 57
|
Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No
|
nodejs Full support 8.10.0 |
sub |
Chrome Full support 68
|
Edge No support 16 — 17
|
Firefox Full support 57
|
IE No support No | Opera No support No | Safari No support 10.1 — ? | WebView Android No support 60 — 63
|
Chrome Android No support 60 — 63
|
Firefox Android Full support 57
|
Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No
|
nodejs Full support 8.10.0 |
wait |
Chrome Full support 68
|
Edge No support 16 — 17
|
Firefox Full support 57
|
IE No support No | Opera No support No | Safari No support 10.1 — ? | WebView Android No support 60 — 63
|
Chrome Android No support 60 — 63
|
Firefox Android Full support 57
|
Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No
|
nodejs Full support 8.10.0 |
xor |
Chrome Full support 68
|
Edge No support 16 — 17
|
Firefox Full support 57
|
IE No support No | Opera No support No | Safari No support 10.1 — ? | WebView Android No support 60 — 63
|
Chrome Android No support 60 — 63
|
Firefox Android Full support 57
|
Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No
|
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.
兼容性提示
直到 Firefox 48,最新的 API 名称和语意都还没有实现。FirFox 46 到 FireFox 48 之间主要有以下变化:
- 移除了方法
Atomics.futexWakeOrRequeue()
和Atomics.fence()
(bug(1259544)、bug(1225028))。 - bug 1262062)
- 移除了属性
Atomics.OK
、Atomics.TIMEDOUT
、Atomics.NOTEQUAL
。bug 1260835)。
相关链接
ArrayBuffer
- JavaScript typed arrays
- Web Workers
- parlib-simple – a simple library providing synchronization and work distribution abstractions.
- Shared Memory – a brief tutorial
- A Taste of JavaScript’s New Parallel Primitives – Mozilla Hacks