C++ From Scratch: std::atomic

แชร์
ฝัง
  • เผยแพร่เมื่อ 29 พ.ย. 2024

ความคิดเห็น •

  • @kobi219040
    @kobi219040 11 หลายเดือนก่อน +3

    Excellent and clear explanation! thanks!

  • @ChadWood-o3z
    @ChadWood-o3z ปีที่แล้ว +5

    Good content, as always.

  • @Krogedam
    @Krogedam ปีที่แล้ว +6

    Excellent video, topic explained very well. Thank you!
    The only thing I did not understand was when you mentioned that there was still undefined behavior; if one thread finished its work before the second thread started.
    Why would this lead to undefined behavior? Would this not be equivalent to doing the work in a single thread, i.e. no race conditions?

    • @Sobioytccc
      @Sobioytccc 11 หลายเดือนก่อน

      The issue is due to a race condition. Atomic operations are one such tool that is lighter than mutexes and their associated locks, such as lock_guard, unique_lock, and condition variables, which are used to prevent race conditions and ensure mutual exclusion. Without atomic operations, it might lead to race conditions. The cause of a race condition is when two threads accessing the same resource, such as a global variable, simultaneously read a value from the RAM, perform an incremental operation, and then store it back in the RAM, which leads to an invalid value.

    • @Sobioytccc
      @Sobioytccc 11 หลายเดือนก่อน +2

      """if one thread finished its work before the second thread started.
      Why would this lead to undefined behavior? Would this not be equivalent to doing the work in a single thread, i.e. no race conditions?"""" it is either he might have said it wrong or you heard it wrong. if first thread starts and finished without second thread even gets a chance to execute, then obviously there is no way undefined behavior will occur.

  • @motionsmoments6254
    @motionsmoments6254 ปีที่แล้ว +5

    nice and simple example

  • @oliveroshea6752
    @oliveroshea6752 หลายเดือนก่อน +1

    thanks Nick!!

  • @yoni532s9M5w
    @yoni532s9M5w ปีที่แล้ว +2

    Very good presentation mate.
    I would love your take on more approaches to deal with this case if any.
    For instance when setting the atomic value, is it slower runtime?

    • @CoffeeBeforeArch
      @CoffeeBeforeArch  ปีที่แล้ว +1

      It's a good idea - I will be handling topics like that in a separate series though (i.e., one specifically on parallel programming).
      Cheers,
      --Nick

    • @anon_y_mousse
      @anon_y_mousse ปีที่แล้ว +2

      While it would run slightly slower, you can't focus on speed in a case like this. It's the difference between yielding an accurate result or crashing. The example here may be simple enough that it would likely never crash, but there are numerous cases where you can really destroy your data by not handling threads correctly. You should practice what he shows in the video and watch the one he did on mutices.

  • @Techomiame
    @Techomiame ปีที่แล้ว +4

    great explanation!

  • @AnhPhan-pr6fs
    @AnhPhan-pr6fs หลายเดือนก่อน

    But why I replace counter += 1 with counter = counter + 1, result is different?

  • @nighttime9539
    @nighttime9539 ปีที่แล้ว +1

    Thanks!

  • @nvtrinh2001
    @nvtrinh2001 10 หลายเดือนก่อน

    Thank you for the video. However, when I use jthread for the same problem, it doesnt produce correct result. I wonder why that's the case?

  • @sarcasticdna
    @sarcasticdna ปีที่แล้ว +1

    This is awesome

  • @depdoprogramming2750
    @depdoprogramming2750 9 หลายเดือนก่อน

    cool video, thank you

  • @0xDEAD_Inside
    @0xDEAD_Inside ปีที่แล้ว +1

    Nice!

  • @qwertyuiop-cu2ve
    @qwertyuiop-cu2ve ปีที่แล้ว

    Why do you pronounce it stood atomic?

    • @CoffeeBeforeArch
      @CoffeeBeforeArch  ปีที่แล้ว +2

      “Stood” is just a common way of pronouncing “std::”