-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainthread.cpp
134 lines (118 loc) · 4.1 KB
/
mainthread.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
#include "include/cache.hpp"
#include "include/fifo_cache_policy.hpp"
#include "include/lifo_cache_policy.hpp"
#include "include/lru_cache_policy.hpp"
#include <iostream>
#include <mutex>
#include <thread>
// mutex to synchronize access to std::cout
std::mutex cout_mutex;
// alias for easy class typing
template <typename Key, typename Value>
using fifo_cache_t = caches::fixed_sized_cache<Key, Value, caches::FIFOCachePolicy>;
template <typename Key, typename Value>
using lifo_cache_t = caches::fixed_sized_cache<Key, Value, caches::LIFOCachePolicy>;
template <typename Key, typename Value>
using lru_cache_t = caches::fixed_sized_cache<Key, Value, caches::LRUCachePolicy>;
void printLine() { std::cout << "==============================================================================\n"; }
void cache_operations_fifo(fifo_cache_t<std::string, int>& cache, const std::string& key, int value, bool insert)
{
if (insert)
{
cache.Put(key, value);
}
else
{
try
{
// lock cout access
std::lock_guard<std::mutex> lock(cout_mutex);
printLine();
std::cout << "Using FIFO Eviction Policy\n"
<< "Value for key '" << key << "': " << cache.Get(key) << '\n';
}
catch (const std::range_error& e)
{
std::cerr << e.what() << '\n';
}
}
}
void cache_operations_lifo(lifo_cache_t<std::string, int>& cache, const std::string& key, int value, bool insert)
{
if (insert)
{
cache.Put(key, value);
}
else
{
try
{
// lock cout access
std::lock_guard<std::mutex> lock(cout_mutex);
printLine();
std::cout << "Using LIFO Eviction Policy\n"
<< "Value for key '" << key << "': " << cache.Get(key) << '\n';
}
catch (const std::range_error& e)
{
std::cerr << e.what() << '\n';
}
}
}
void cache_operations_lru(lru_cache_t<std::string, int>& cache, const std::string& key, int value, bool insert)
{
if (insert)
{
cache.Put(key, value);
}
else
{
try
{
// lock cout access
std::lock_guard<std::mutex> lock(cout_mutex);
printLine();
std::cout << "Using LRU Eviction Policy\n"
<< "Value for key '" << key << "': " << cache.Get(key) << '\n';
}
catch (const std::range_error& e)
{
std::cerr << e.what() << '\n';
}
}
}
int main()
{
std::cout << "Hello Enterpret!\n";
constexpr std::size_t CACHE_SIZE = 256;
fifo_cache_t<std::string, int> fifo_cache(CACHE_SIZE);
lifo_cache_t<std::string, int> lifo_cache(CACHE_SIZE);
lru_cache_t<std::string, int> lru_cache(CACHE_SIZE);
// creating multiple threads to perform cache operations concurrently
std::thread t1(cache_operations_fifo, std::ref(fifo_cache), "Hello", 100, true);
std::thread t2(cache_operations_fifo, std::ref(fifo_cache), "world", 6996, true);
std::thread t3(cache_operations_fifo, std::ref(fifo_cache), "Hello", 0, false);
std::thread t4(cache_operations_fifo, std::ref(fifo_cache), "world", 0, false);
std::thread t5(cache_operations_lifo, std::ref(lifo_cache), "Hello", 200, true);
std::thread t6(cache_operations_lifo, std::ref(lifo_cache), "world", 7997, true);
std::thread t7(cache_operations_lifo, std::ref(lifo_cache), "Hello", 0, false);
std::thread t8(cache_operations_lifo, std::ref(lifo_cache), "world", 0, false);
std::thread t9(cache_operations_lru, std::ref(lru_cache), "Hello", 300, true);
std::thread t10(cache_operations_lru, std::ref(lru_cache), "world", 8998, true);
std::thread t11(cache_operations_lru, std::ref(lru_cache), "Hello", 0, false);
std::thread t12(cache_operations_lru, std::ref(lru_cache), "world", 0, false);
// joining the threads and waiting for their completion
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
t6.join();
t7.join();
t8.join();
t9.join();
t10.join();
t11.join();
t12.join();
return 0;
}