Naši dobrovolníci ještě tento článek do jazyka Čeština nepřeložili. Přidejte se a pomozte nám tuto práci dokončit!
Tento článek si můžete přečíst také v jazyce English (US).

The Atomics object provides atomic operations as static methods. They are used with SharedArrayBuffer objects.

The Atomic operations are installed on an Atomics module. Unlike the other global objects, Atomics is not a constructor. You cannot use it with a new operator or invoke the Atomics object as a function. All properties and methods of Atomics are static (as is the case with the Math object, for example).

Properties

Atomics[Symbol.toStringTag]
The value of this property is "Atomics".

Methods

Atomic operations

When memory is shared, multiple threads can read and write the same data in memory. Atomic operations make sure that predictable values are written and read, that operations are finished before the next operation starts and that operations are not interrupted.

Atomics.add()
Adds a given value at a given position in the array. Returns the old value at that position.
Atomics.and()
Computes a bitwise AND at a given position in the array. Returns the old value at that position.
Atomics.compareExchange()
Stores a given value at a given position in the array, if it equals a given value. Returns the old value.
Atomics.exchange()
Stores a given value at a given position in the array. Returns the old value.
Atomics.load()
Returns the value at the given position in the array.
Atomics.or()
Computes a bitwise OR at a given position in the array. Returns the old value at that position.
Atomics.store()
Stores a given value at the given position in the array. Returns the value.
Atomics.sub()
Subtracts a given value at a given position in the array. Returns the old value at that position.
Atomics.xor()
Computes a bitwise XOR at a given position in the array. Returns the old value at that position.

Wait and wake

The wait() and wake() methods are modeled on Linux futexes ("fast user-space mutex") and provide ways for waiting until a certain condition becomes true and are typically used as blocking constructs.

Atomics.wait()

Verifies that a given position in the array still contains a given value and sleeps awaiting or times out. Returns either "ok", "not-equal", or "timed-out". If waiting is not allowed in the calling agent then it throws an Error exception (most browsers will not allow wait() on the browser's main thread).

Atomics.wake()
Wakes up some agents that are sleeping in the wait queue on the given array position. Returns the number of agents that were woken up.
Atomics.isLockFree(size)

An optimization primitive that can be used to determine whether to use locks or atomic operations. Returns true, if an atomic operation on arrays of the given element size will be implemented using a hardware atomic operation (as opposed to a lock). Experts only.

Specifications

Specification Status Comment
ECMAScript Latest Draft (ECMA-262)
The definition of 'Atomics' in that specification.
Living Standard Initial definition in ES2017.
ECMAScript 2017 (ECMA-262)
The definition of 'Atomics' in that specification.
Standard  

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic Support6016

55

46 — 541

No No10.1
add6016

55

46 — 541

No No10.1
and6016

55

46 — 541

No No10.1
compareExchange6016

55

46 — 541

No No10.1
exchange6016

55

46 — 541

No No10.1
isLockFree6016

55

46 — 541

No No10.1
load6016

55

46 — 541

No No10.1
or6016

55

46 — 541

No No10.1
store6016

55

46 — 541

No No10.1
sub6016

55

46 — 541

No No10.1
wait6016

55

46 — 541 2

No No10.1
wake60316

55

46 — 541 4

No No10.1
xor6016

55

46 — 541

No No10.1
FeatureAndroidChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
Basic Support6060 ?

55

46 — 541

No No No
add6060 ?

55

46 — 541

No No No
and6060 ?

55

46 — 541

No No No
compareExchange6060 ?

55

46 — 541

No No No
exchange6060 ?

55

46 — 541

No No No
isLockFree6060 ?

55

46 — 541

No No No
load6060 ?

55

46 — 541

No No No
or6060 ?

55

46 — 541

No No No
store6060 ?

55

46 — 541

No No No
sub6060 ?

55

46 — 541

No No No
wait6060 ?

55

46 — 541 2

No No No
wake6060 ?

55

46 — 541 4

No No No
xor6060 ?

55

46 — 541

No No No

1. From version 46 until version 54 (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.

2. In versions 46 and 47, this method was named Atomics.futexWait() and the properties Atomics.OK, Atomics.TIMEDOUT, Atomics.NOTEQUAL were returned instead of the strings.

3. The count parameter defaults to 0, instead of +Infinity.

4. In versions 46 and 47, this method was named Atomics.futexWake() and the count parameter defaulted to 0.

Compatibility notes

Prior to Firefox 48, the latest API names and semantics weren't implemented yet. The changes between Firefox version 46 and version 48 are:

See also

Štítky a přispěvatelé do dokumentace

 Poslední aktualizace od: fscholz,