0Pricing
CUDA Academy · درس

عمليات ذرية مخصصة باستخدام atomicCAS

المقارنة والاستبدال لأي تحديث.

عمليات ذرية مخصصة باستخدام atomicCAS درس مجاني في CUDA Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في CUDA Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة CUDA Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

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

الأسئلة الشائعة

هل درس «عمليات ذرية مخصصة باستخدام atomicCAS» مجاني؟

نعم — نص درس «عمليات ذرية مخصصة باستخدام atomicCAS» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة CUDA Academy، انتقل إلى CoddyKit PRO. تتضمن دورة CUDA Academy 4 دروس في المجموع.

ماذا ستتعلم في «عمليات ذرية مخصصة باستخدام atomicCAS»؟

المقارنة والاستبدال لأي تحديث. تتمرن على CUDA Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ CUDA Academy؟

لا تُشترط خبرة سابقة. CUDA Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «عمليات ذرية مخصصة باستخدام atomicCAS»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس CUDA Academy هذا؟

نعم. كل درس في CUDA Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. حالات التسابق على GPU
  2. atomicAdd وما شابهها
  3. بناء Histogram
  4. عمليات ذرية مخصصة باستخدام atomicCAS
← العودة إلى CUDA Academy