0Pricing
CUDA Academy · Lesson

Custom Atomics with atomicCAS

Compare-and-swap for any update.

Custom Atomics with atomicCAS is a free CUDA Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CUDA Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

When Built-Ins Run Out

The built-in atomics cover add, max, exchange and more. But what if you need an atomic update they do not provide? Meet atomicCAS. 🔁

Compare-And-Swap

atomicCAS means compare-and-swap. It writes a new value only if the current value matches what you expected, all in one atomic step.

atomicCAS(addr, expected, desired);

What It Returns

atomicCAS returns the value that was actually stored. If it equals your expected value, your swap succeeded; otherwise someone else changed it first.

The Retry Loop

The core pattern is a loop: read the current value, compute the new one, try the swap, and repeat until atomicCAS confirms success.

A CAS Loop in Code

This loop keeps retrying until no other thread sneaks in between your read and your write. That is the heart of a custom atomic.

do {
  old = *addr;
  newv = f(old);
} while (atomicCAS(addr, old, newv) != old);

Build Any Operation

Put any function in place of f, and you have an atomic version of it. CAS lets you build updates the hardware never shipped directly.

Atomic Float on Old Hardware

Before native float atomics, people emulated atomicAdd for floats using atomicCAS on the bit pattern via __int_as_float tricks.

Why the Loop Can Spin

Under heavy contention the swap often fails and retries. Each failure means another thread won, so hot addresses make CAS loops spin longer.

Progress Is Guaranteed

Even with retries, the algorithm is lock-free: whenever a swap fails, some other thread definitely made progress. The system never stalls.

Match the Width

atomicCAS works on specific widths like 32 and 64 bits. Cast your data to the matching integer type so the compare operates on raw bits.

Prefer Built-Ins First

Reach for a built-in atomic when one exists; it is faster and simpler. Use atomicCAS only for the custom updates nothing else covers.

Quick Check

Check your understanding of compare-and-swap.

Recap: Custom Atomics

You learned that atomicCAS plus a retry loop builds any atomic update, stays lock-free, but can spin under contention. ✅

Frequently asked questions

Is the “Custom Atomics with atomicCAS” lesson free?

Yes — the full text of “Custom Atomics with atomicCAS” is free to read here on the web, and the CUDA Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CUDA Academy course, upgrade to CoddyKit PRO.

What will I learn in “Custom Atomics with atomicCAS”?

Compare-and-swap for any update. You practise CUDA Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start CUDA Academy?

No prior experience is required. CUDA Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Custom Atomics with atomicCAS” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this CUDA Academy lesson?

Yes. Every CUDA Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Race Conditions on the GPU
  2. atomicAdd and Friends
  3. Building a Histogram
  4. Custom Atomics with atomicCAS
← Back to CUDA Academy