---
title: "atomicCompareExchangeWeak: shader function guide"
description: "A weak compare-exchange conditionally replaces a shared integer when it matches an expected value. It reports the old value and whether replacement occurred."
url: https://typeshade.dev/reference/functions/atomicCompareExchangeWeak/
language: en
---

# `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](https://typeshade.dev/reference/functions/atomicCompareExchangeWeak/#targets)

## Compare examples

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

**Model assumptions**`location: atomic<u32>`

**Example 1`atomicCompareExchangeWeak(location, 5, 3)`**

**Stored value before**

`5`

**Result**

`({ old_value: 5, exchanged: true }, stored = 3) ({ old_value: 5, exchanged: false }, stored = 5)`

**Example 2`atomicCompareExchangeWeak(location, 1, 1)`**

**Stored value before**

`0`

**Result**

`({ old_value: 0, exchanged: false }, stored = 0)`

**Example 3`atomicCompareExchangeWeak(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](https://www.w3.org/TR/WGSL/) and [GLSL ES 3.00](https://registry.khronos.org/OpenGL/specs/es/3.0/GLSL_ES_Specification_3.00.pdf).

## Related functions

- [`atomicLoad`](https://typeshade.dev/reference/functions/atomicLoad/)
- [`atomicStore`](https://typeshade.dev/reference/functions/atomicStore/)
- [`atomicAdd`](https://typeshade.dev/reference/functions/atomicAdd/)
- [`atomicSub`](https://typeshade.dev/reference/functions/atomicSub/)
- [`atomicMin`](https://typeshade.dev/reference/functions/atomicMin/)

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

```ts
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

- [`gallery/compute-sync/compute-sync.shade.ts`](https://typeshade.dev/guide/examples/compute-sync/)

## See also

- [Functions](https://typeshade.dev/reference/functions/)
- [Builtin table](https://typeshade.dev/guide/language/builtins/#atomics)
