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

  • @piyushpathak1186
    @piyushpathak1186 Жыл бұрын

    I pay my internet bill for this type of content

  • @Sanjay-pu4sp
    @Sanjay-pu4sp Жыл бұрын

    You have a real gift for teaching mate. Thanks so much!

  • @viktoreidrien7110
    @viktoreidrien7110 Жыл бұрын

    Superb video, thanks.

  • @adrishray9179
    @adrishray91799 ай бұрын

    Very nicely explained. Love it when when ppl use pens and drawings to teach. The internet needs this type of content

  • @divyachauhan3478
    @divyachauhan34789 ай бұрын

    I learned this coming from non cse background. So amazing content

  • @prateekraj1084
    @prateekraj10846 ай бұрын

    Awesome explanation of such a building block concept.

  • @princekha4540
    @princekha4540 Жыл бұрын

    Thanks for the video, bro 🙌

  • @shishirchaurasiya7374
    @shishirchaurasiya737410 ай бұрын

    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 ❤

  • @yogeshedekar6078
    @yogeshedekar60787 ай бұрын

    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.

  • @MOHDSALMAN-sj2zu
    @MOHDSALMAN-sj2zu Жыл бұрын

    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?

  • @rahulsangvikar7973
    @rahulsangvikar7973Ай бұрын

    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?

  • @anishdabhane9132
    @anishdabhane913221 күн бұрын

    awesome

  • @CipherNL
    @CipherNL Жыл бұрын

    As if it's not complicated enough, you chose a hard to read font?

  • @rishabhsharma5050
    @rishabhsharma5050 Жыл бұрын

    So whats the solution to make it safe

  • @AsliEngineering

    @AsliEngineering

    Жыл бұрын

    Locking - optimistic and pessimistic. Videos on those topics are already out on my channel.

  • @rajibmajumder9564

    @rajibmajumder9564

    11 ай бұрын

    std::atomic count; count++;

Келесі