Peeking into assembly code to understand why count++ is not atomic
Ғылым және технология
System Design for SDE-2 and above: arpitbhayani.me/masterclass
System Design for Beginners: arpitbhayani.me/sys-design
Redis Internals: arpitbhayani.me/redis
Build Your Own Redis / DNS / BitTorrent / SQLite - with CodeCrafters.
Sign up and get 40% off - app.codecrafters.io/join?via=...
In this video, I delved into why a seemingly simple operation like count++, when executed by two threads simultaneously, can lead to inconsistencies in the final value. By examining the Assembly Language code generated from a basic C program, I uncovered that count++ is not atomic due to the underlying micro operations involved. Despite the appearance of atomicity in the Assembly Language code, the actual execution involves multiple micro operations, making count++ non-atomic. Understanding these intricacies sheds light on the beauty of computer architecture and the reasons behind non-atomic behaviors.
Recommended videos and playlists
If you liked this video, you will find the following videos and playlists helpful
System Design: • PostgreSQL connection ...
Designing Microservices: • Advantages of adopting...
Database Engineering: • How nested loop, hash,...
Concurrency In-depth: • How to write efficient...
Research paper dissections: • The Google File System...
Outage Dissections: • Dissecting GitHub Outa...
Hash Table Internals: • Internal Structure of ...
Bittorrent Internals: • Introduction to BitTor...
Things you will find amusing
Knowledge Base: arpitbhayani.me/knowledge-base
Bookshelf: arpitbhayani.me/bookshelf
Papershelf: arpitbhayani.me/papershelf
Other socials
I keep writing and sharing my practical experience and learnings every day, so if you resonate then follow along. I keep it no fluff.
LinkedIn: / arpitbhayani
Twitter: / arpit_bhayani
Weekly Newsletter: arpit.substack.com
Thank you for watching and supporting! it means a ton.
I am on a mission to bring out the best engineering stories from around the world and make you all fall in
love with engineering. If you resonate with this then follow along, I always keep it no-fluff.
Пікірлер: 16
I pay my internet bill for this type of content
You have a real gift for teaching mate. Thanks so much!
Superb video, thanks.
Very nicely explained. Love it when when ppl use pens and drawings to teach. The internet needs this type of content
I learned this coming from non cse background. So amazing content
Awesome explanation of such a building block concept.
Thanks for the video, bro 🙌
I am watching the series the second time, and I can't explain how much clarity in my brain these concepts are getting injected, amazing arpit sir ❤
This reminded me of my Microprocessor practiclas where we would code using JMP for loops on a set of keyboard and small monitor. Amazing though, hard to find such deapth for any of the concurrency lectures.
Hi Arpit, Thank you for this explanation. It was really helpful. Could you please also provide an explanation of how the count++ and ++count expressions execute in the context of multithreading?
Why do we need 2 mov micro instructions? Why couldn't we just use something like addl $1, %rdi as a single micro instruction? Also, adding just 1 to a number "binarily" is extremely efficient right? It's much faster than adding any other number, so why couldn't we handle this as a special case?
awesome
As if it's not complicated enough, you chose a hard to read font?
So whats the solution to make it safe
@AsliEngineering
Жыл бұрын
Locking - optimistic and pessimistic. Videos on those topics are already out on my channel.
@rajibmajumder9564
11 ай бұрын
std::atomic count; count++;