aboutsummaryrefslogtreecommitdiff
path: root/store/works/life/operating-system-experiment/DataRaceDemo.cpp
blob: a2f98d9502e908db3efc3716ecadd106d4a0d320 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "Thread.h"

#include <iostream>

int main() {
  unsigned data = 0;

  cru::Thread t1([&data] {
    for (int i = 0; i < 100000; i++)
      data += 10;
  });

  cru::Thread t2([&data] {
    for (int i = 0; i < 100000; i++)
      data += 10;
  });

  std::cout << "Created thread: " << t1.GetNativeID() << '\n';
  std::cout << "Created thread: " << t2.GetNativeID() << '\n';

  t1.Join();
  t2.Join();
  std::cout << "Answer is " << data << ", which is "
            << (data == 2000000 ? "correct" : "false") << '\n';

  return 0;
}