-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataMemory.cpp
More file actions
37 lines (27 loc) · 946 Bytes
/
DataMemory.cpp
File metadata and controls
37 lines (27 loc) · 946 Bytes
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
#include "DataMemory.h"
DataMemory::DataMemory(int size):data_mem_size(size) {
data_memory = new int[size];
// Initialize it all to 0s
for (int i = 0; i < size; i++) {
data_memory[i] = 0;
}
}
DataMemory::~DataMemory() {
delete[] data_memory;
}
int DataMemory::readVal(int index) {
std::lock_guard<std::mutex> guard(mutex);
return data_memory[index];
}
void DataMemory::writeVal(int index, int val) {
std::lock_guard<std::mutex> guard(mutex);
data_memory[index] = val;
}
// Used for debugging only, will call it in main in the end after joining all threads, so no point of using mutex here
void DataMemory::printDataMem(int index) {
std::cout << "Will print the content of the Data Memory up to index number " << index << ":" << std::endl;
for (int k = 0; k < index; k++) {
std::cout << " data_mem[" << k << "] = " << data_memory[k];
}
std::cout << std::endl << std::endl;
}