Understanding std::atomic::compare_exchange_weak() in C++11

后端 未结 5 1420
花落未央
花落未央 2020-12-07 08:47
bool compare_exchange_weak (T& expected, T val, ..);

compare_exchange_weak() is one of compare-exchange primitives provided in C++

5条回答
  •  日久生厌
    2020-12-07 09:08

    Alright, so I need a function which performs atomic left-shifting. My processor doesn't have a native operation for this, and the standard library doesn't have a function for it, so it looks like I'm writing my own. Here goes:

    void atomicLeftShift(std::atomic* var, int shiftBy)
    {
        do {
            int oldVal = std::atomic_load(var);
            int newVal = oldVal << shiftBy;
        } while(!std::compare_exchange_weak(oldVal, newVal));
    }
    

    Now, there's two reasons that loop might be executed more than once.

    1. Someone else changed the variable while I was doing my left shift. The results of my computation should not be applied to the atomic variable, because it would effectively erase that someone else's write.
    2. My CPU burped and the weak CAS spuriously failed.

    I honestly don't care which one. Left shifting is fast enough that I may as well just do it again, even if the failure was spurious.

    What's less fast, though, is the extra code that strong CAS needs to wrap around weak CAS in order to be strong. That code doesn't do much when the weak CAS succeeds... but when it fails, strong CAS needs to do some detective work to determine whether it was Case 1 or Case 2. That detective work takes the form of a second loop, effectively inside my own loop. Two nested loops. Imagine your algorithms teacher glaring at you right now.

    And as I previously mentioned, I don't care about the result of that detective work! Either way I'm going to be redoing the CAS. So using strong CAS gains me precisely nothing, and loses me a small but measurable amount of efficiency.

    In other words, weak CAS is used to implement atomic update operations. Strong CAS is used when you care about the result of CAS.

提交回复
热议问题