/////////////////////////////////////////////////////////////// // Atomics.cpp - atomic operations on integral types // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2013 // /////////////////////////////////////////////////////////////// #include #include #include #include #include std::mutex mtx; void up(std::atomic& i) { srand(time(nullptr)); for(int j=0; j<20; ++j) { std::this_thread::sleep_for(std::chrono::microseconds(rand() % 100)); ++i; // atomic operation std::lock_guard l(mtx); // guard for I/O integrity std::cout << "\n t - value of atomic is " << i.load(); // could use i since guarded } } int main() { std::cout << "\n Demonstrating atomic"; std::cout << "\n =========================\n"; std::atomic i = 0; if (i.is_lock_free()) std::cout << "\n atomic is lock free"; else std::cout << "\n atomic is not lock free"; std::cout << "\n"; std::thread t(up, std::ref(i)); // shared count srand(time(nullptr)); for(int j=0; j<8; ++j) { std::this_thread::sleep_for(std::chrono::microseconds(rand()%1400)); --i; std::lock_guard l(mtx); std::cout << "\n m - value of atomic is " << i.load(); } t.join(); std::cout << "\n value of atomic is " << i; std::cout << "\n\n"; }