---
title: "atomicExchange / InterlockedExchange: shader function guide"
description: "Replaces an integer memory location atomically. It returns the value from before the update. Use it for swapping a shared integer."
url: https://typeshade.dev/reference/functions/atomicExchange/
language: en
---

# `atomicExchange`

Function

Replaces an integer memory location atomically. It returns the value from before the update.

**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.

- HLSL: `InterlockedExchange`

[Targets](https://typeshade.dev/reference/functions/atomicExchange/#targets)

## Compare examples

Compare the returned value with the stored value. Each case starts from its own initial state.

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

**Example 1`atomicExchange(location, 3)`**

**Stored value before**

`5`

**Returned value**

`5`

**Stored value after**

`3`

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

**Stored value before**

`0`

**Returned value**

`0`

**Stored value after**

`1`

**Example 3`atomicExchange(location, 1)`**

**Stored value before**

`4294967295`

**Returned value**

`4294967295`

**Stored value after**

`1`

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 for swapping a shared integer.

## Inputs and limits

The model uses an unsigned integer and one serialized operation. Real invocation order is unspecified. Atomicity does not synchronize unrelated memory; these calls require WebGPU in this compiler.

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 `value` in the atomic location as one indivisible step and returns the value it held before.

## Syntax

```ts
declare function atomicExchange<T extends u32 | i32>(location: atomic<T>, value: T): T
```

## Parameters

- `location` `atomic<T>`
- `value` `T`

## Return value

`T`

## Description

Compiles to `atomicExchange(&bins[i], v)` on WGSL; the GLSL lowering in a render entry is not supported yet ([Rule 10.5](https://typeshade.dev/reference/rules/10-5/)), and a `@compute` entry that reaches no texture runs on WebGL2 as its pass program ([Rule 8.24](https://typeshade.dev/reference/rules/8-24/), [Rule 11.8](https://typeshade.dev/reference/rules/11-8/)).

## Targets

**WGSL**

`atomicExchange(&a, b)`

**GLSL ES 3.00**

No form on this target.

## See also

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