-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
52 lines (44 loc) · 1.73 KB
/
main.cpp
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "BankAccount.h"
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main() {
const int nThreads = 10;
const int nRuns = 10;
mutex mtx; // synchronized access to standard output cout
BankAccount account; // synchronized bank account
double unsynchronizedAccount = 0; // unsychronized bank account
thread t[nThreads]; // thread pool
// parallel task
auto task = [nRuns,&account,&mtx,&unsynchronizedAccount] {
srand((unsigned int)hash<thread::id>()(this_thread::get_id())); // ensures that all threads have a different seed for the random number generator
for (int i = 0; i < nRuns; i++) {
if (i & 1) {
const double amount = rand()*1000/RAND_MAX;
const double b = unsynchronizedAccount + amount;
account.deposit(amount);
unsynchronizedAccount = b;
mtx.lock();
cout << "thread " << this_thread::get_id() << " deposits " << amount << endl;
mtx.unlock();
}
const double balance = account.getBalance();
mtx.lock();
cout << "thread " << this_thread::get_id() << ": balance is = " << balance << ", unsychronized balance is = " << unsynchronizedAccount << endl;
cout << "concurrent readers: " << account.getReaders() << endl;
mtx.unlock();
}
};
cout << "main thread id = " << this_thread::get_id() << ", hw concurrency = " << thread::hardware_concurrency() <<endl;
// start threads
for (int i = 0; i < nThreads; i++) {
t[i] = thread(task); // thread uses move-semantics: t[i] will run the task and the temporary thread will immediately finish
}
// join threads: main thread waits for parallel threads
for (int i = 0; i < nThreads; i++) {
cout << "wait until thread " << t[i].get_id() << " has finished" << endl;
t[i].join();
}
system("pause");
}