Cbeam
Loading...
Searching...
No Matches
thread_safe_container.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
28#include <cbeam/error/runtime_error.hpp> // for cbeam::error:runtime_error
29#include <cbeam/lifecycle/scoped_set.hpp> // for cbeam::lifecycle::scoped_set
30
31#include <cassert> // for assert
32#include <cstddef> // for std::size_t
33
34#include <atomic> // for std::atomic
35#include <mutex> // for std::recursive_mutex, std::lock_guard
36#include <string> // for std::string, std::allocator
37
38namespace cbeam::container
39{
43 template <typename T>
45 {
46 public:
54 {
55 std::recursive_mutex& _mutex;
56
57 public:
61 explicit lock_guard(std::recursive_mutex& mutex)
62 : _mutex(mutex)
63 {
64 _mutex.lock();
65 }
66
68 ~lock_guard() noexcept
69 {
70 _mutex.unlock();
71 }
72
73 // Deleted copy constructor and assignment operator to prevent copying.
74 lock_guard(const lock_guard&) = delete;
75 lock_guard& operator=(const lock_guard&) = delete;
76 };
77
88 {
90 {
91 throw cbeam::error::runtime_error("cbeam::container::thread_safe_container::get_lock_guard: not allowed during destruction of the container.");
92 }
93 return lock_guard(_mutex);
94 }
95
98
99 virtual ~thread_safe_container() noexcept
100 {
101 assert(!_is_being_modified && "cbeam::container::thread_safe_container: destruction during modification or duplicate destruction");
102 _is_being_modified = true;
103 }
104
108 bool empty() const
109 {
110 std::lock_guard<std::recursive_mutex> lock(_mutex);
111 return _container.empty();
112 }
113
115 void clear()
116 {
117 std::lock_guard<std::recursive_mutex> lock(_mutex);
119 {
120 throw cbeam::error::runtime_error("cbeam::container::thread_safe_container::clear: not allowed during modification of the container.");
121 }
123 _container.clear();
124 }
125
129 std::size_t size() const
130 {
131 std::lock_guard<std::recursive_mutex> lock(_mutex);
132 return _container.size();
133 }
134
136 auto begin()
137 {
138 std::lock_guard<std::recursive_mutex> lock(_mutex);
139 return _container.begin();
140 }
141
143 auto end()
144 {
145 std::lock_guard<std::recursive_mutex> lock(_mutex);
146 return _container.end();
147 }
148
149 bool is_being_modified() const
150 {
151 return _is_being_modified;
152 }
153
154 protected:
155 mutable std::recursive_mutex _mutex;
157 std::atomic<bool> _is_being_modified{false};
158 };
159}
Inner class providing RAII-style locking mechanism.
Definition thread_safe_container.hpp:54
lock_guard & operator=(const lock_guard &)=delete
lock_guard(std::recursive_mutex &mutex)
Constructs the lock guard and locks the provided mutex.
Definition thread_safe_container.hpp:61
~lock_guard() noexcept
Destructor that unlocks the mutex.
Definition thread_safe_container.hpp:68
T _container
Internal non-thread safe container.
Definition thread_safe_container.hpp:156
void clear()
Clears the contents.
Definition thread_safe_container.hpp:115
std::atomic< bool > _is_being_modified
Definition thread_safe_container.hpp:157
lock_guard get_lock_guard() const
Acquires a lock guard for the set, ensuring thread safety.
Definition thread_safe_container.hpp:87
bool empty() const
Checks if the container is empty.
Definition thread_safe_container.hpp:108
thread_safe_container()=default
Default constructor.
std::size_t size() const
Returns the number of elements.
Definition thread_safe_container.hpp:129
auto begin()
Returns an iterator to the beginning.
Definition thread_safe_container.hpp:136
bool is_being_modified() const
Definition thread_safe_container.hpp:149
auto end()
Returns an iterator to the end.
Definition thread_safe_container.hpp:143
std::recursive_mutex _mutex
Mutex to protect access to _container.
Definition thread_safe_container.hpp:155
virtual ~thread_safe_container() noexcept
Definition thread_safe_container.hpp:99
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