---
title: "atomicCompareExchangeWeak / atomicCompSwap: 셰이더 함수 설명과 예제"
description: "공유 정수가 예상값과 같으면 새 값으로 바꾸기를 시도합니다. 기존 값과 교체 성공 여부를 돌려줍니다. 공유 정수를 조건부로 갱신하는 재시도 루프에 사용합니다. 예상값이 같다고 성공이 보장되지는 않습니다. 알고리즘에 따라 재시도가 필요합니다."
url: https://typeshade.dev/ko/reference/functions/atomicCompareExchangeWeak/
language: ko
---

# `atomicCompareExchangeWeak`

함수

공유 정수가 예상값과 같으면 새 값으로 바꾸기를 시도합니다. 기존 값과 교체 성공 여부를 돌려줍니다.

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

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

- GLSL: `atomicCompSwap`

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

## 예제 비교

예상값이 다르면 교체에 실패합니다. 예상값이 같아도 교체에 성공하는 경우와 일시적으로 실패하는 경우가 모두 허용됩니다.

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

**예제 1`atomicCompareExchangeWeak(location, 5, 3)`**

**연산 전 저장값**

`5`

**결과**

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

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

**연산 전 저장값**

`0`

**결과**

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

**예제 3`atomicCompareExchangeWeak(location, 4294967295, 1)`**

**연산 전 저장값**

`4294967295`

**결과**

`({ old_value: 4294967295, exchanged: true }, stored = 1) ({ old_value: 4294967295, exchanged: false }, stored = 4294967295)`

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

## 활용

공유 정수를 조건부로 갱신하는 재시도 루프에 사용합니다.

## 입력 범위와 주의사항

예상값이 같다고 성공이 보장되지는 않습니다. 알고리즘에 따라 재시도가 필요합니다. 모델은 일치할 때 허용되는 두 결과를 함께 보여주며 GPU 스케줄러를 모사하지 않습니다.

셰이더의 입력 규칙과 정밀도는 [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 the third argument into the atomic location only when it holds the second, as one indivisible step.

## 구문

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

## 매개변수

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

## 반환값

`{ old_value: T; exchanged: bool }`

## 설명

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.

## 대상별 출력

**WGSL**

`atomicCompareExchangeWeak(&a, b, c)`

**GLSL ES 3.00**

`atomicCompareExchangeWeak` is not supported yet in a GLSL ES 3.00 stage (Rule 10.5)

## 예제

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

## 함께 보기

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