-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ALTV-634 [server] Refactor Resource utilization
- Loading branch information
1 parent
4aa3e96
commit e13f8ab
Showing
3 changed files
with
86 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#pragma once | ||
|
||
#include <mutex> | ||
#include <unordered_map> | ||
#include <optional> | ||
|
||
/// \brief Safe thread associative container | ||
/// \tparam K type of key | ||
/// \tparam V type of value | ||
template<class K, class V> | ||
class ThreadSafeContainer final | ||
{ | ||
public: | ||
ThreadSafeContainer() = default; | ||
~ThreadSafeContainer() noexcept = default; | ||
|
||
/// \brief Insert value in container by key | ||
/// \param K key | ||
/// \param V value | ||
void Insert(K key, V value); | ||
|
||
/// \brief Check value exists by key | ||
/// \param K key | ||
/// \return true - if value exists, else - false | ||
bool Find(K key) const; | ||
|
||
/// \brief Get element from container | ||
/// \param K key | ||
/// \return value | ||
std::optional<V> GetElement(K key) const; | ||
|
||
private: | ||
std::unordered_map<K, V> container; | ||
mutable std::mutex mutex; | ||
|
||
ThreadSafeContainer(ThreadSafeContainer&& other) = delete; | ||
ThreadSafeContainer(const ThreadSafeContainer& other) = delete; | ||
ThreadSafeContainer& operator=(ThreadSafeContainer&& other) = delete; | ||
ThreadSafeContainer& operator=(const ThreadSafeContainer& other) = delete; | ||
}; | ||
|
||
template<class K, class V> | ||
void ThreadSafeContainer<K, V>::Insert(K key, V value) | ||
{ | ||
std::lock_guard lock{ mutex }; | ||
container[key] = value; | ||
} | ||
|
||
template<class K, class V> | ||
bool ThreadSafeContainer<K, V>::Find(K key) const | ||
{ | ||
std::lock_guard lock{ mutex }; | ||
return container.find(key) != container.end(); | ||
} | ||
|
||
template<class K, class V> | ||
std::optional<V> ThreadSafeContainer<K, V>::GetElement(K key) const | ||
{ | ||
std::lock_guard lock{ mutex }; | ||
auto it = container.find(key); | ||
if (it == container.end()) | ||
{ | ||
return std::nullopt; | ||
} | ||
return it->second; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters