/////////////////////////////////////////////////////////////// // Atomics.cpp - atomic operations on integral types // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2013 // /////////////////////////////////////////////////////////////// #include #include #include #include std::mutex mtx; void up(std::atomic& i) { for(int j=0; j<20; ++j) { i += 1; std::lock_guard l(mtx); std::cout << "\n t - value of atomic is " << i; } } int main() { std::atomic i = 0; std::thread t(up, std::ref(i)); for(int j=0; j<8; ++j) { static bool wait = true; if(wait) std::this_thread::sleep_for(std::chrono::milliseconds(1)); wait = false; i -= 1; std::lock_guard l(mtx); std::cout << "\n m - value of atomic is " << i; } t.join(); std::cout << "\n value of atomic is " << i; std::cout << "\n\n"; }