///////////////////////////////////////////////////////////////// // LockGuard.cpp - ensures unlocking in exception environment // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2013 // ///////////////////////////////////////////////////////////////// #include #include #include std::mutex mtx; void test() { for(int i=0; i<5; ++i) { { std::lock_guard guard(mtx); std::cout << "\n in test()"; } std::this_thread::sleep_for(std::chrono::milliseconds(100)); } } int main() { std::thread t(test); for(int i=0; i<5; ++i) { { std::lock_guard guard(mtx); std::cout << "\n in main()"; } std::this_thread::sleep_for(std::chrono::milliseconds(200)); } t.join(); std::cout << "\n\n"; }