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