-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNamedItemStore.h
More file actions
84 lines (65 loc) · 2.51 KB
/
NamedItemStore.h
File metadata and controls
84 lines (65 loc) · 2.51 KB
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
#pragma once
#include "pch.h"
#include <map>
#include <vector>
#include <tuple>
namespace ndtech {
template <typename ItemType>
struct NamedItemStore {
std::map<std::string, ItemType> m_items;
std::mutex m_itemsMutex;
std::condition_variable m_itemsCV;
std::vector<std::tuple<std::string, ItemType, std::string>> m_itemActions;
std::mutex m_itemActionsMutex;
bool m_running = true;
void RunOnThread(std::thread& thread) {
thread = std::thread(&NamedItemStore::Run, this);
}
void Run() {
while (m_running) {
{
std::lock_guard lock(m_itemActionsMutex);
for (auto itemAction : m_itemActions) {
if (std::get<2>(itemAction) == "a") {
std::lock_guard itemsLock(m_itemsMutex);
m_items.insert_or_assign(std::get<0>(itemAction), std::get<1>(itemAction));
m_itemsCV.notify_all();
std::cout << "NamedItemStore threadId = " << std::this_thread::get_id() << ": added item itemName = " << std::get<0>(itemAction) << " itemValue = " << std::get<1>(itemAction) << std::endl;
}
else if (std::get<2>(itemAction) == "r") {
std::lock_guard itemsLock(m_itemsMutex);
auto foundItem = m_items.find(std::get<0>(itemAction));
if (foundItem != m_items.end()) {
m_items.erase(foundItem);
m_itemsCV.notify_all();
std::cout << "NamedItemStore threadId = " << std::this_thread::get_id() << ": removed item itemName = " << std::get<0>(itemAction) << std::endl;
}
else {
std::cout << "NamedItemStore threadId = " << std::this_thread::get_id() << ": unable to remove item itemName = " << std::get<0>(itemAction) << " because it does not exist." << std::endl;
}
}
}
m_itemActions.clear();
}
}
}
void Stop() {
m_running = false;
}
void AddItem(std::string key, ItemType value) {
std::lock_guard lock(m_itemActionsMutex);
m_itemActions.push_back(std::make_tuple(key, value, "a"));
}
void RemoveItem(std::string itemName) {
std::lock_guard lock(m_itemActionsMutex);
m_itemActions.push_back(std::make_tuple(itemName, "", "r"));
}
auto GetItem(std::string itemName) {
std::unique_lock lock(m_itemsMutex);
while (m_items.find(itemName) == m_items.end()) {
m_itemsCV.wait(lock);
}
return m_items.find(itemName);
}
};
}