-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultithread_cache.cpp
59 lines (49 loc) · 1.93 KB
/
multithread_cache.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
#include <chrono> // std::chrono_literals
#include <iostream> // std::cout
#include <mutex> // std::mutex
#include <string> // std::string
#include <thread> // std::thread, std::this_thread::sleep_for()
#include "Cache/Cache.h" // class Cache
#include "Cache/Policy/LRU.h" // Policy::LRU (LRU replacement policy)
using namespace std::chrono_literals;
// We begin by creating a global cache that both threads will be able to access.
// This could be a private class member, but for sake of simplicity we will keep
// it as a global variable.
// For this example, we will be creating a 128-entry string-int cache with LRU
// replacement policy and std::mutex as the mutex to be used by this class. This
// last template parameter is what makes it thread-safe. By deafult, this parameter
// is NullLock, which is a dummy class that does nothing but provide the std::mutex
// interface. This makes it really quick in single-thread scenarios but totally
// unsafe for multithreaded environments.
Cache<std::string, int, Policy::LRU, std::mutex> cache(128);
// This is the first thread. It will only be checking a value from cache, but in
// a real application both read, modify and write operations are supported
// simultaneously in any arbitrary number of threads (provided the caches are
// thread-safe as explained above).
void thread1()
{
for(int i = 0; i < 10; i++)
{
std::this_thread::sleep_for(100ms);
std::cout << "key 'asd' = " << cache["asd"] << std::endl;
}
}
// This is our second thread. It will be modifying the cache after half a second,
// but as explained before, any operation can be performed as long as the cache
// is thread-safe.
void thread2()
{
std::this_thread::sleep_for(500ms);
cache["asd"] = 24;
}
int main()
{
// To start with, we will insert "asd" -> 42 into the cache
cache["asd"] = 42;
// Spawn both threads
std::thread t1(thread1);
std::thread t2(thread2);
// Wait for them to finish
t1.join();
t2.join();
}