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