Atomic operations are indivisible.

Compare and Swap

int compare_and_swap (int* reg, int oldval, int newval) {
	int old_reg_val = *reg;
	if (old_reg_val == oldval)
	*reg = newval;
	return old_reg_val;
}

We basically check if old_reg_val is returned here to see if our operation was successful. This is an important atomic operation since it combines a read, comparison, and write into a single operation.

Spinlocks

A spinlock is something that is constantly trying to acquire a lock. This makes sense if the expected wait time to acquire the lock is less than the time it would take for two threads to switch. You use compare_and_swap() during this implementation.

ABA Problem

When you read a location twice and see the same value each time, it could have switched values in between checks. We can use a modification count to track this?

In all atomic blocks, the running threads checks to make sure nothing that was read was modified by another thread. If this succeeds, then changes are committed.