How You Should Use Mutexes And Atomic Values In Golang?!

Поделиться
HTML-код
  • Опубликовано: 31 янв 2025

Комментарии • 26

  • @anthonygg_
    @anthonygg_  2 года назад +1

    ► Join my Discord community for free education 👉 discord.com/invite/bDy8t4b3Rz
    ► Become a Patreon for exclusive tutorials👉 www.patreon.com/anthonygg_
    ► Buy me a coffee 👉 donate.stripe.com/aEU2a6ayH2uCa3u4gg
    Thanks for watching

  • @nsgirish
    @nsgirish Год назад +14

    Hands down. This is one of the most informative golang videos i have come across in the last 5-6 years. Thanks sir. your style of explaining by writing code on fly is tough to do which shows your command in the language but very informative for us. Pls keep up your great work and share your great knowledge on other languages like c++ and Rust too.

  • @joeyhills212
    @joeyhills212 9 месяцев назад +4

    This is the best mutex explanation ive seem. Makes perfect sense now thanks man

  • @LNMLucasMasiero
    @LNMLucasMasiero 3 месяца назад

    Nice explanation mate! And an flowly understandable example ❤

  • @RootsterAnon
    @RootsterAnon Год назад +1

    Very nice and informative video. I never knew what Mutex is and what Atomic is but now I get to understand it and when and why use each. Tnx!

  • @ericrichard4482
    @ericrichard4482 2 года назад +1

    Thanks for the video, new to Golang and was waiting for you to make video on Mutexs :P

  • @DeepakKumar-ze8ii
    @DeepakKumar-ze8ii 4 месяца назад

    Great example, thanks Anthony 👍

  • @oneke4498
    @oneke4498 Год назад +1

    Nice tutorial everything explained clean!

  • @tpotjj2979
    @tpotjj2979 2 года назад +1

    Haven't covered this subject, greate one!

  • @sovrinfo
    @sovrinfo 2 года назад +1

    Thanks for the video!

  • @thanhvo8798
    @thanhvo8798 2 года назад +13

    Mutex is slower than Atomic, b/c it blocks other gorountines. Just go with Atomic

  • @JarodLatta-b6s
    @JarodLatta-b6s Год назад +6

    I'm a little unclear on how the atomicity is maintained when you have one atomic load to fetch the health, then an atomic store to set the new health. What would happen if two goroutines tried to update the health at the same time?
    A: atomic read health (e.g. 10)
    B: atomic read health (e.g. 10)
    A: atomic write health = 10 - 1 -> 9
    B: atomic write health = 10 - 2 -> 8
    Final result should be 7, but it's 8. Is this not how it works? Or is the atomicity somehow maintained between calls? Unless there's an atomic increment/decrement operation, it seems like the better choice for this situation would be mutex even if it's slower

    • @DavidFregoli
      @DavidFregoli Год назад +4

      you are right, he should have used atomic.AddInt32

  • @oleksiistri8429
    @oleksiistri8429 8 месяцев назад

    Great explanation, thanks. One more question, can you mix mutex and atomic? Will they respect each other? For example in one goroutine use mutex to modify whole struct and in another goroutine modify only one member of that struct with help of atomic?

  • @hocky-ham324-zg8zc
    @hocky-ham324-zg8zc Год назад +2

    8:30 - Isn’t this wrong because the loop will break before unlocking the mutex? You’d want to add another Unlock() call right before the break right?

  • @alexmeh1919
    @alexmeh1919 Год назад

    💥💥💥

  • @Amine-gz7gq
    @Amine-gz7gq 10 месяцев назад

    just use message passing so we can maintain your code. don't use synchronization primitives.