#include #include #include #include // For chrono::milliseconds using namespace std ; mutex myMutex ; int shared_data = 0; void worker_function1(int id) { try { myMutex.lock() ; int x1 = 3 ; for (int i1 = 1; i1 <= 5; ++i1) { cout << "worker_function1: i1 " << i1 << endl ; if ( i1 == x1 ) throw x1 ; } //for myMutex.unlock() ; } catch( int& e1 ) { } } void worker_function2(int id) { try { myMutex.lock() ; int x1 = 3 ; for (int i1 = 1; i1 <= 5; ++i1) { cout << "worker_function2: i1 " << i1 << endl ; if ( i1 == x1 ) throw x1 ; } //for myMutex.unlock() ; } catch( int& e1 ) { } } int main() { thread t1(worker_function1, 1); thread t2(worker_function2, 2); t1.join(); t2.join(); cout << "Final shared_data: " << shared_data << endl; return 0; }