-
Notifications
You must be signed in to change notification settings - Fork 1
/
thread_practice.hpp
60 lines (47 loc) · 1.69 KB
/
thread_practice.hpp
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
53
54
55
56
57
58
59
// Name: Joshua McCarville-Schueths
// File: pthread_practice.h
// Description: This program demonstrates creating threads,
// passing data to threads, and using mutual
// exclusion to prevent concurrency issues. The
// way the program does that is by simulating
// a shared set of savings and checking accounts
// for a three member family.
#include <fstream>
#include <iostream>
#include <string>
#include <mutex>
// Thread that is allows parallel processing.
void* family_member(const std::string& file_name);
// Reads a transaction file and applies each one.
void processTransactions(const std::string& name, std::ifstream& in);
// Prints the current account balances.
void printBalances();
// Global Variables for Threads //
int savings = 0; // Savings account value.
int checking = 0; // Checking account value.
std::mutex savingsLock; // Mutex for savings.
std::mutex checkingLock; // Mutex for checking.
std::mutex transferLock; // Mutex to prevent transfer deadlock.
class Transaction;
std::istream& operator>>(std::istream& in, Transaction& transaction);
enum class Operation {
Deposit,
Withdrawal,
Transfer
};
enum class AccountType {
Checking,
Savings
};
std::ostream& operator<<(std::ostream& out, AccountType type);
class Transaction {
public:
Operation operation() const { return mOperation; }
AccountType account() const { return mAccount; }
int amount() const { return mAmount; }
friend std::istream& operator>>(std::istream& in, Transaction& transaction);
private:
Operation mOperation {Operation::Deposit};
AccountType mAccount {AccountType::Checking};
int mAmount {0};
};