I was recently reading about the [Compare And Swap](http://en.wikipedia.org/wiki/Compare-and-swap) atomic action (CMPXCHG, .NET's Interlocked.CompareExchange, whatever).

I understand how it works internally, and how it's used from a client.

What I can't quite figure out is when would someone use CAS?

Wikipedia says:

> 
> CAS is used for implementing synchronization primitives like
> semaphores and mutexes, likewise more sophisticated lock-free and
> wait-free algorithms.
> 
> 
> 

So, can anyone give me a more generic real-world use case with code and description of CAS usage?

This question is meant to be language-agnostic, so any language will do (C-based or x86 assembly preferred).

Thanks!