atomicCompareExchangeWeak
On this page

atomicCompareExchangeWeak

Function

A weak compare-exchange conditionally replaces a shared integer when it matches an expected value. It reports the old value and whether replacement occurred.

Related names in other languages

These related operations can differ in arguments, support, and semantics. The foreign names are not accepted TypeShade aliases. Check the target forms and restrictions.

  • GLSL: atomicCompSwap

Targets

Compare examples

Compare matching and mismatching expected values. Even a match permits a spurious failure.

Model assumptionslocation: atomic<u32>

Example 1atomicCompareExchangeWeak(location, 5, 3)
Stored value before
5
Result
({ old_value: 5, exchanged: true }, stored = 3) ({ old_value: 5, exchanged: false }, stored = 5)
Example 2atomicCompareExchangeWeak(location, 1, 1)
Stored value before
0
Result
({ old_value: 0, exchanged: false }, stored = 0)
Example 3atomicCompareExchangeWeak(location, 4294967295, 1)
Stored value before
4294967295
Result
({ old_value: 4294967295, exchanged: true }, stored = 1) ({ old_value: 4294967295, exchanged: false }, stored = 4294967295)

The examples are calculation models and do not run GPU work. The reference below gives the complete call signatures. Decimal results are rounded for display. The stated assumptions determine each example; actual shader results follow the target precision and execution rules.

When to use it

Use it in retry loops for conditional updates of a shared integer.

Inputs and limits

A matching expected value does not guarantee success. Retry according to the algorithm; the model lists both allowed matching outcomes. It does not simulate a GPU scheduler.

For shader input rules and precision, consult WGSL and GLSL ES 3.00.

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

Stores the third argument into the atomic location only when it holds the second, as one indivisible step.

Syntax

declare function atomicCompareExchangeWeak<T extends u32 | i32>(location: atomic<T>, compare: T, value: T): { old_value: T; exchanged: bool }

Parameters

  • location atomic<T>
  • compare T
  • value T

Return value

{ old_value: T; exchanged: bool }

Description

Answers a struct: old_value is what the location held before the call and exchanged says whether the store happened. WGSL gives that struct no writable name, so bind the result with const and read its fields.

Targets

WGSL
atomicCompareExchangeWeak(&a, b, c)
GLSL ES 3.00
atomicCompareExchangeWeak is not supported yet in a GLSL ES 3.00 stage (Rule 10.5)

Examples

See also

Edit this page Report a problem