Question about atomic operations ...
Take, say, increment, the following is done:
1)грузим в регистр значение переменной 2)инкрементируем 3)выгружаем из регистра в память If we start doing this from different streams, then we can read the old value, this is understandable ...
However, with the help of some kind of magic, atomic types from different streams allow this to be turned without errors? What does an atomic type mean? Is this property of a certain type or can it be guaranteed for any type?
Well, the code:
#include <iostream> #include <atomic> #include <thread> #include <chrono> int main() { using namespace std::chrono_literals; //std::atomic<long long> i{1}; //std::atomic<long long> ii{2}; long long i = 1; long long ii = 2; std::thread t1([&](){ i++; i--; ii++; }); std::thread t2([&](){ i++; i--; ii++; }); std::thread t3([&](){ i++; i--; ii++; }); std::thread t4([&](){ i++; i--; ii++; }); t1.join(); t2.join(); t3.join(); t4.join(); std::this_thread::sleep_for(100ms); std::cout<<i<<" "<<ii; } why what for atomic types, what for non-atomic types always shows 1 6 ... there should not be such