1 #include <iostream> 2 #include <mutex> 3 #include <thread> 4 5 int bar(int i) { 6 int j = i * i; 7 return j; // break here 8 } 9 10 int foo(int i) { return bar(i); } 11 12 void call_and_wait(int &n) { 13 std::cout << "waiting for computation!" << std::endl; 14 while (n != 42 * 42) 15 ; 16 std::cout << "finished computation!" << std::endl; 17 } 18 19 void compute_pow(int &n) { n = foo(n); } 20 21 int main() { 22 int n = 42; 23 std::mutex mutex; 24 std::unique_lock<std::mutex> lock(mutex); 25 26 std::thread thread_1(call_and_wait, std::ref(n)); 27 std::thread thread_2(compute_pow, std::ref(n)); 28 lock.unlock(); 29 30 thread_1.join(); 31 thread_2.join(); 32 33 return 0; 34 } 35