---
title: "atomicExchange / InterlockedExchange: 셰이더 함수 설명과 예제"
description: "공유 정수를 새 값으로 바꾸고 갱신 전의 값을 돌려줍니다. 공유 정수를 새 값으로 교체할 때 사용합니다. 모델은 부호 없는 정수에 연산 하나를 순서대로 적용합니다. 실제 호출 순서는 정해져 있지 않습니다. 원자성은 다른 메모리를 동기화하지 않습니다."
url: https://typeshade.dev/ko/reference/functions/atomicExchange/
language: ko
---

# `atomicExchange`

함수

공유 정수를 새 값으로 바꾸고 갱신 전의 값을 돌려줍니다.

**다른 언어의 관련 이름**

관련 연산은 인수, 지원 범위와 의미가 다를 수 있습니다. 다른 언어의 이름을 TypeShade 별칭으로 사용할 수는 없습니다. 지원하는 형태와 제한은 대상 언어에 따라 다릅니다.

- HLSL: `InterlockedExchange`

[대상별 출력](https://typeshade.dev/ko/reference/functions/atomicExchange/#targets)

## 예제 비교

기존 값과 관계없이 입력값이 저장됩니다. 반환값에는 교체 전의 값이 남습니다.

**모델의 전제**`location: atomic<u32>`

**예제 1`atomicExchange(location, 3)`**

**연산 전 저장값**

`5`

**반환값**

`5`

**연산 후 저장값**

`3`

**예제 2`atomicExchange(location, 1)`**

**연산 전 저장값**

`0`

**반환값**

`0`

**연산 후 저장값**

`1`

**예제 3`atomicExchange(location, 1)`**

**연산 전 저장값**

`4294967295`

**반환값**

`4294967295`

**연산 후 저장값**

`1`

예제는 설명용 계산 모델이며 GPU를 실행하지 않습니다. 정확한 호출 형식은 아래 컴파일러 참고 자료에서 설명합니다. 소수 결과는 표시할 때 반올림했습니다. 각 예제는 명시한 전제를 따릅니다. 실제 셰이더의 결과는 대상 언어의 정밀도와 실행 규칙에 따릅니다.

## 활용

공유 정수를 새 값으로 교체할 때 사용합니다.

## 입력 범위와 주의사항

모델은 부호 없는 정수에 연산 하나를 순서대로 적용합니다. 실제 호출 순서는 정해져 있지 않습니다. 원자성은 다른 메모리를 동기화하지 않습니다. 이 컴파일러에서는 WebGPU가 필요합니다.

셰이더의 입력 규칙과 정밀도는 [WGSL](https://www.w3.org/TR/WGSL/)과 [GLSL ES 3.00](https://registry.khronos.org/OpenGL/specs/es/3.0/GLSL_ES_Specification_3.00.pdf)에서 규정합니다.

## 관련 함수

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

시그니처와 이름 아래 문장, 컴파일러 메시지는 커밋 af54ddad5의 컴파일러가 쓴 영어 원문입니다.

Stores `value` in the atomic location as one indivisible step and returns the value it held before.

## 구문

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

## 매개변수

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

## 반환값

`T`

## 설명

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/ko/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/ko/reference/rules/8-24/), [Rule 11.8](https://typeshade.dev/ko/reference/rules/11-8/)).

## 대상별 출력

**WGSL**

`atomicExchange(&a, b)`

**GLSL ES 3.00**

이 대상에는 형태가 없습니다.

## 함께 보기

- [함수](https://typeshade.dev/ko/reference/functions/)
- [내장 함수 표](https://typeshade.dev/ko/guide/language/builtins/#atomics)
