brintos

brintos / llvm-project-archived public Read only

0
0
Text · 638 B · b7900b5 Raw
36 lines · cpp
1#include <iostream>2#include <mutex>3#include <thread>4 5int bar(int i) {6  int *j = 0;7  *j = 1;8  return i; // break here9}10 11int foo(int i) { return bar(i); }12 13void 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 20void compute_pow(int &n) { n = foo(n); }21 22int 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(n));28  std::thread thread_2(compute_pow, std::ref(n));29  lock.unlock();30 31  thread_1.join();32  thread_2.join();33 34  return 0;35}36