Eric
Eric

Reputation: 24924

Compare and swap - What if 2 processors execute locking simultaneous?

I read about CAS in https://en.wikipedia.org/wiki/Compare-and-swap, and got some doubts:

Anyone help to explain these doubts? Thanks.

Upvotes: 2

Views: 562

Answers (1)

user4822941
user4822941

Reputation:

The cpu has a cache for memory, typically 64 bytes of size per so-called cache line. It will do stuff with respect to chunks of that size. In particular, when doing lock cmpxchg or similar things, the hardware thread you execute this on will negotiate exclusive access to the 64-byte portion of memory with other threads. And that's why it works.

In general, you want to read this book: https://www.kernel.org/pub/linux/kernel/people/paulmck/perfbook/perfbook.html

This particular bit is explained on page 21.

Regarding the LKD quote, there is no context provided. It is safe to assume they meant threads and were updating a thread-local counter.

Upvotes: 2

Related Questions