Cbeam
Loading...
Searching...
No Matches
thread_safe_set.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2025 acrion innovations GmbH
3Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7Cbeam is offered under a commercial and under the AGPL license.
8For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10AGPL licensing:
11
12Cbeam is free software: you can redistribute it and/or modify
13it under the terms of the GNU Affero General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17Cbeam is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU Affero General Public License for more details.
21
22You should have received a copy of the GNU Affero General Public License
23along with Cbeam. If not, see <https://www.gnu.org/licenses/>.
24*/
25
26#pragma once
27
29#include <cbeam/error/runtime_error.hpp> // for cbeam::error:runtime_error
30#include <cbeam/lifecycle/scoped_set.hpp> // for cbeam::lifecycle::scoped_set
31
32#include <cassert> // for assert
33#include <cstddef> // for std::size_t
34
35#include <atomic> // for std::atomic
36#include <mutex> // for std::recursive_mutex, std::lock_guard
37#include <set> // for std::set
38#include <utility> // for std::pair
39
40namespace cbeam::container
41{
45 template <typename T>
46 class thread_safe_set : public thread_safe_container<std::set<T>>
47 {
48 public:
60 template <typename... Args>
61 std::pair<typename std::set<T>::iterator, bool> emplace(Args&&... args)
62 {
63 std::lock_guard<std::recursive_mutex> lock(this->_mutex);
64 if (this->_is_being_modified)
65 {
66 throw cbeam::error::runtime_error("cbeam::container::thread_safe_set::emplace: not allowed during modification of the set.");
67 }
68 lifecycle::scoped_set modifying(this->_is_being_modified, true);
69 return this->_container.emplace(std::forward<Args>(args)...);
70 }
71
80 bool insert(const T& value)
81 {
82 std::lock_guard<std::recursive_mutex> lock(this->_mutex);
83 if (this->_is_being_modified)
84 {
85 throw cbeam::error::runtime_error("cbeam::container::thread_safe_set::insert: not allowed during modification of the set.");
86 }
88 auto result = this->_container.insert(value);
89 return result.second;
90 }
91
100 bool erase(const T& value)
101 {
102 std::lock_guard<std::recursive_mutex> lock(this->_mutex);
103 if (this->_is_being_modified)
104 {
105 throw cbeam::error::runtime_error("cbeam::container::thread_safe_set::erase: not allowed during modification of the set.");
106 }
108 return this->_container.erase(value) > 0;
109 }
110
119 bool contains(const T& value) const
120 {
121 std::lock_guard<std::recursive_mutex> lock(this->_mutex);
122 return this->_container.find(value) != this->_container.end();
123 }
124 };
125}
T _container
Definition thread_safe_container.hpp:156
std::atomic< bool > _is_being_modified
Definition thread_safe_container.hpp:157
std::recursive_mutex _mutex
Definition thread_safe_container.hpp:155
Thread-safe wrapper for std::set.
Definition thread_safe_set.hpp:47
std::pair< typename std::set< T >::iterator, bool > emplace(Args &&... args)
Constructs an element in-place within the underlying std::set.
Definition thread_safe_set.hpp:61
bool insert(const T &value)
Inserts a copy of value into the underlying std::set.
Definition thread_safe_set.hpp:80
bool erase(const T &value)
Erases a value from the set if it exists.
Definition thread_safe_set.hpp:100
bool contains(const T &value) const
Checks whether the specified value is contained in the set.
Definition thread_safe_set.hpp:119
A Cbeam-specific runtime error that also acts like std::runtime_error.
Definition runtime_error.hpp:46
A helper that sets the given variable to a new value, and restores the original value on destruction.
Definition scoped_set.hpp:61
Offers advanced container types with unique approaches to stability and interprocess sharing....
Definition buffer.hpp:44