Previous | Next --- Slide 31 of 66
Back to Lecture Thumbnails
potato

How are atomic objects implemented under the hood to always avoid deadlock, as is easy to do when we as programmers use locks, semaphores, etc.?

kv1

This stackoverflow response provides a pretty good discussion on how atomic objects are implemented. It looks like on modern processors, things like incrementing an atomic int are compiled down to single instructions (i.e., the processor supports atomic increment instructions).

I think the high-level answer though is it depends on the system (since the atomic keyword is just the programmer specifying they want operations on a certain variable to be atomic, and there's no explicit requirement for how this is implemented under the hood).

sareyan

@potato looking at the previous slide mention of load-linked / store-conditional is potentially illuminating for me on one way all this atomic stuff might be implemented in theory, even if the processor didn't have atomic instructions

You could set a bit on the addresses you read from, and then if another thread modifies those addresses before you have completed your atomic work, re-read and restart your work. I guess reading multiple bits and deciding if any were tainted would still need to be an atomic instruction. This idea is reminiscent to me of the idea of read sets in the transactions lecture.

Please log in to leave a comment.