-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_map.cpp
157 lines (116 loc) · 3.91 KB
/
test_map.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "threadsafe_data_structures.hpp"
#include <iostream>
#include <map>
#include <string>
#include <thread>
#include <vector>
#include <chrono>
#include <exception>
void insert_numbers_map(const int start, ThreadsafeMap<int, std::string> &safe_map) {
for(int i = start; i < start + 100; ++i) {
safe_map.insert_or_update(i, std::to_string(i));
}
}
void erase_numbers_map(const int start, ThreadsafeMap<int, std::string> &safe_map) {
try {
for(int i = start; i < start + 100; ++i) {
safe_map.erase(i);
}
}
catch(std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
}
void insert_numbers_set(const int start, ThreadsafeSet<int> &safe_set) {
for(int i = start; i < start + 100; ++i) {
safe_set.insert(i);
}
}
void erase_numbers_set(const int start, ThreadsafeSet<int> &safe_set) {
try {
for(int i = start; i < start + 100; ++i) {
safe_set.erase(i);
}
}
catch(std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
}
int main() {
ThreadsafeMap<int, std::string> safe_map;
std::vector<std::thread> threads;
std::map<int, std::string> halfway_map, final_map;
int num_threads = 16;
for(int i = 0; i < num_threads; ++i) {
threads.push_back(std::thread(insert_numbers_map, i * 100, std::ref(safe_map)));
}
halfway_map = safe_map.get_map();
for(int i = 0; i < num_threads; ++i) {
threads[i].join();
}
std::cout << "\n\nHALFWAY MAP AFTER ADDING SOME NUMBERS:" << std::endl;
for(const auto &it: halfway_map)
std::cout << "{" << it.first << ", " << it.second << "}, ";
std::cout << "\n";
final_map = safe_map.get_map();
std::cout << "\n\nFINAL MAP AFTER ADDING ALL NUMBERS:" << std::endl;
for(const auto &it: final_map)
std::cout << "{" << it.first << ", " << it.second << "}, ";
std::cout << "\n";
threads = std::vector<std::thread>();
for(int i = 0; i < num_threads; ++i) {
threads.push_back(std::thread(erase_numbers_map, i * 50, std::ref(safe_map)));
}
for(int i = 0; i < num_threads; ++i) {
threads[i].join();
}
std::cout << "\n\nFINAL MAP AFTER ERASING SOME NUMBERS:" << std::endl;
final_map = safe_map.get_map();
for(const auto &it: final_map)
std::cout << "{" << it.first << ", " << it.second << "}, ";
std::cout << "\n";
std::cout << "\n\nTRYING TO GET ALL THE NUMBERS FROM 0 TO NUM_THREADS * 100" << std::endl;
for(int i = 0; i < num_threads * 100; ++i) {
auto val = safe_map.get(i, "NOT_FOUND");
std::cout << "{" << val << "}, ";
}
std::cout << "\n";
ThreadsafeSet<int> safe_set;
threads = std::vector<std::thread>();
std::set<int> halfway_set, final_set;
for(int i = 0; i < num_threads; ++i) {
threads.push_back(std::thread(insert_numbers_set, i * 100, std::ref(safe_set)));
}
halfway_set = safe_set.get_set();
for(int i = 0; i < num_threads; ++i) {
threads[i].join();
}
std::cout << "\n\nHALFWAY SET AFTER ADDING SOME NUMBERS:" << std::endl;
for(const auto &it: halfway_set)
std::cout << "{" << it << "}, ";
std::cout << "\n";
final_set = safe_set.get_set();
std::cout << "\n\nFINAL SET AFTER ADDING ALL NUMBERS:" << std::endl;
for(const auto &it: final_set)
std::cout << "{" << it << "}, ";
std::cout << "\n";
threads = std::vector<std::thread>();
for(int i = 0; i < num_threads; ++i) {
threads.push_back(std::thread(erase_numbers_set, i * 50, std::ref(safe_set)));
}
for(int i = 0; i < num_threads; ++i) {
threads[i].join();
}
std::cout << "\n\nFINAL SET AFTER ERASING SOME NUMBERS:" << std::endl;
final_set = safe_set.get_set();
for(const auto &it: final_set)
std::cout << "{" << it << "}, ";
std::cout << "\n";
std::cout << "\n\nTRYING TO GET ALL THE NUMBERS FROM 0 TO NUM_THREADS * 100" << std::endl;
for(int i = 0; i < num_threads * 100; ++i) {
auto val = safe_set.contains(i);
std::cout << "{" << val << "}, ";
}
std::cout << "\n";
return 0;
}