Learn the vocabulary of an atomic instruction that only writes a new value if it still matches what's expected.
0 / 5 completed
1 / 5
At standup, a dev mentions a single atomic CPU instruction that reads a memory location, compares it to an expected value, and only writes a new value if the two still match, all as one indivisible step. What is this instruction called?
Compare-and-swap, or CAS, is exactly this single atomic instruction: it reads a memory location, compares it to an expected value, and only writes a new value if the two still match, with the whole read-compare-write sequence happening as one indivisible step that no other thread can interrupt partway through. A hash collision is an unrelated hash-table concept about two keys sharing a bucket. This atomic read-compare-write behavior is exactly what lets concurrent code update shared memory safely without ever needing a traditional lock.
2 / 5
During a design review, the team builds a lock-free counter using compare-and-swap in a retry loop, reading the current value, computing the new value, and calling CAS, retrying from the start whenever another thread's CAS wins the race first. Which capability does this retry loop provide?
This retry loop provides correctness under concurrent updates without ever blocking a thread with a traditional lock, since if another thread's compare-and-swap wins the race and changes the value first, this thread's own CAS simply fails, and it retries by rereading the now-current value and computing again, rather than corrupting the counter or requiring a lock at all. Simply writing the new value directly with no comparison would let two threads silently overwrite each other's updates. This detect-and-retry behavior is exactly what makes CAS the foundation of lock-free data structures.
3 / 5
In a code review, a dev notices a shared counter is updated by reading its current value, computing an incremented value, and writing it back directly with no atomic compare-and-swap step protecting the write. What does this represent?
This is a race condition, since without an atomic compare-and-swap step, two threads can both read the exact same current value, both independently compute the same incremented value, and then one thread's write can silently overwrite the other's, meaning one of the two increments is lost entirely even though both threads believed they succeeded. A cache eviction policy is an unrelated concept about discarded cache entries. This unprotected read-then-write pattern is exactly the kind of concurrency bug a compare-and-swap-based update, retrying whenever the value has changed underneath it, is designed to prevent.
4 / 5
An incident report shows a shared request counter under-reported traffic in production, because it was updated by reading the current value, incrementing it, and writing it back directly with no atomic protection, letting concurrent updates silently overwrite each other. What practice would prevent this?
Updating the counter with a compare-and-swap loop, so a losing update simply rereads the now-current value and retries instead of silently overwriting another thread's increment, guarantees no update is ever lost, which is exactly the fix for the under-reporting described in this incident. Continuing to read, increment, and write the counter back directly with no atomic protection is exactly what let concurrent updates silently clobber each other. This detect-and-retry pattern, built on compare-and-swap, is the standard, lock-free fix for exactly this class of lost-update race condition.
5 / 5
During a PR review, a teammate asks why the team reaches for a compare-and-swap retry loop instead of just protecting the counter update with a traditional lock, given that both approaches prevent the same lost-update race condition. What is the reasoning?
A compare-and-swap loop avoids ever blocking a thread outright, since a losing attempt just retries immediately with a fresh read instead of putting the thread to sleep the way a traditional lock does under contention, which can scale better when many threads are updating the same value concurrently. The tradeoff is that the update has to be structured as a retryable read-compute-write step, which is straightforward for something like a counter but can get considerably more intricate for a more complex shared structure. This is exactly why compare-and-swap is favored for simple, hot, frequently-contended updates, while a traditional lock remains simpler to reason about for more complex critical sections.
What does the "Compare-and-Swap Vocabulary" vocabulary exercise cover?
This exercise tests real IT vocabulary related to compare-and-swap vocabulary through 5 multiple-choice questions, each built from realistic workplace sentences rather than abstract definitions.
Is this vocabulary exercise free to use?
Yes. Every exercise on CoderSlingo, including this one, is completely free — no account, sign-up, or payment required.
How many questions does this exercise have?
This exercise has 5 questions. Each one shows a real-world sentence or scenario with multiple-choice options and an explanation once you answer.
What happens after I answer a question?
You'll see immediate feedback showing whether your answer was correct, along with a short explanation of why — then a button to move to the next question, and a full results screen at the end.
Can I retry the exercise if I get questions wrong?
Yes. Once you reach the results screen, click "Try again" to reset your answers and go through the exercise from the start as many times as you like.
Do I need to create an account to take this exercise?
No account is needed. Your answers are scored in your browser during the session — nothing is saved to a server, so you can jump straight in.
Is my progress saved if I leave the page?
No — progress within an exercise resets if you navigate away or reload. Each exercise is short enough to complete in a few minutes in one sitting.
Are these vocabulary exercises connected to other topics?
Yes — this module shares real-world context with 9 other vocabulary modules. See "Related vocabulary" below to keep building a connected skill set.
How is this different from reading a glossary or blog article?
Exercises like this one are active recall drills — you have to choose the correct term or phrasing yourself, which builds retention faster than passively reading a definition.
Where can I find more vocabulary exercises?
Browse the full Vocabulary exercises hub for hundreds of modules covering Agile, DevOps, security, databases, architecture, and more — organised by IT role and skill.