xtd 1.0.0
Loading...
Searching...
No Matches
concurrent_bag.hpp
Go to the documentation of this file.
1
4#pragma once
9#include "../../as.hpp"
10#include "../../lock_guard.hpp"
11#include "../../new_ptr.hpp"
12#include <atomic>
13
15namespace xtd {
17 namespace collections {
20 namespace concurrent {
36 template<typename type_t>
38 public:
40
55
57
60 concurrent_bag() = default;
63 concurrent_bag(std::initializer_list<type_t> items) {
64 for (const auto& item : items)
65 add(item);
66 }
67
69 for (const auto& item : collection)
70 add(item);
71 }
72
73
75
79 [[nodiscard]] auto count() const noexcept -> xtd::usize override {return as<xtd::usize>(count_);}
80
83 [[nodiscard]] auto is_empty() const noexcept -> bool {return count_ == 0;}
84
88 [[nodiscard]] auto is_read_only() const noexcept -> bool override {return false;}
89
104 [[nodiscard]] auto is_synchronized() const noexcept -> bool override {return true;}
106
108
114
119 auto copy_to(xtd::array<value_type>& array, xtd::usize array_index) const -> void override {
121 static thread_local auto items = xtd::array<value_type> {};
122 items = to_array();
123 for (auto i = xtd::usize {0}; i < count(); ++i)
124 array[array_index + i] = items[i];
125 }
126
129 [[nodiscard]] auto get_enumerator() const -> xtd::collections::generic::enumerator<value_type> override {
130 static thread_local auto items = xtd::array<value_type> {};
131 items = to_array();
132 return items.get_enumerator();
133 }
134
138 [[nodiscard]] auto to_array() const -> xtd::array<value_type> override {
140 lock_guard_(storages_) {
141 for (const auto& [thread_id, storage] : storages_)
142 result.add_range(*storage);
143 }
144 return result.to_array();
145 }
146
149 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {return xtd::string::format("[{}]", xtd::string::join(", ", self_));}
150
155 auto try_add(const_reference item) -> bool override {
156 current_local_storage()->enqueue(item);
158 return true;
159 }
160
164 auto try_peek(reference item) const -> bool {
165 if (current_local_storage()->count()) return current_local_storage()->try_peek(item);
166 lock_guard_(storages_) {
167 for (auto& [thread_id, storage] : storages_) {
168 if (storage->count()) return storage->try_peek(item);
169 }
170 }
171 return false;
172 }
173
177 auto try_take(reference item) -> bool override {
178 if (current_local_storage()->count()) {
179 auto result = current_local_storage()->try_dequeue(item);
180 if (result) {
182 return true;
183 }
184 }
185 lock_guard_(storages_) {
186 for (auto& [thread_id, storage] : storages_) {
187 if (storage->count()) {
188 auto result = storage->try_dequeue(item);
189 if (result) {
191 return true;
192 }
193 }
194 }
195 }
196 return false;
197 }
198
199 private:
201 [[nodiscard]] auto contains(const_reference item) const noexcept -> bool override {return false;}
202 auto remove(const_reference item) -> bool override {return false;}
203 [[nodiscard]] auto sync_root() const noexcept -> const object& override {return storages_;}
204
205 auto current_local_storage() const -> local_storage_type {
206 if (thread_local_storages_.contains_key(instance_id_)) return thread_local_storages_[instance_id_];
207 lock_guard_(storages_) {
208 auto id = xtd::threading::thread::current_thread().thread_id();
209 if (!storages_.contains_key(id)) storages_.add(id, new_ptr<collection_type>());
210 auto storage = storages_[id].get();
211 thread_local_storages_[instance_id_] = storage;
212 return storage;
213 }
214 return thread_local_storages_[instance_id_];
215 }
216
217 inline static thread_local xtd::collections::generic::dictionary<xtd::uint64, local_storage_type> thread_local_storages_;
218 inline static std::atomic<xtd::uint64> next_id_;
219 mutable storage_type storages_;
220 xtd::int64 count_ = 0;
221 xtd::uint64 instance_id_ = ++next_id_;
222 };
223
225 // Deduction guides for xtd::collections::concurrent::concurrent_bag
226 // {
227 template<typename type_t>
228 concurrent_bag(std::initializer_list<type_t>) -> concurrent_bag<type_t>;
229
230 template<typename type_t>
231 concurrent_bag(const xtd::collections::generic::ienumerable<type_t>&) -> concurrent_bag<type_t>;
232 // }
234 }
235 }
236}
Contains xtd::as method.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
virtual auto length() const noexcept -> size_type
Gets a size that represents the total number of elements in all the dimensions of the array.
Definition basic_array.hpp:122
static auto join(const basic_string &separator, const collection_t &values) noexcept -> basic_string
Definition basic_string.hpp:1258
Represents a thread-safe, unordered collection of objects.
Definition concurrent_bag.hpp:37
auto is_synchronized() const noexcept -> bool override
Gets a value indicating whether access to the xtd::collections::concurrent::concurrent_bag <type_t> i...
Definition concurrent_bag.hpp:104
auto get_enumerator() const -> xtd::collections::generic::enumerator< value_type > override
Returns an enumerator that iterates through a collection.
Definition concurrent_bag.hpp:129
xtd::collections::generic::dictionary< xtd::intptr, xtd::ptr< collection_type > > storage_type
Represents the concurrent bag collection type.
Definition concurrent_bag.hpp:53
auto count() const noexcept -> xtd::usize override
Gets the number of elements contained in the xtd::collections::generic::icollection.
Definition concurrent_bag.hpp:79
auto add(const_reference item) -> void override
Adds an item to the xtd::collections::concurrent::concurrent_bag <type_t>.
Definition concurrent_bag.hpp:113
auto try_add(const_reference item) -> bool override
Attempts to add an object to the xtd::collections::concurrent::concurrent_bag <type_t>.
Definition concurrent_bag.hpp:155
auto is_empty() const noexcept -> bool
Gets a value indicating whether the xtd::collections::concurrent::concurrent_bag <type_t> is emtpy.
Definition concurrent_bag.hpp:83
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current object.
Definition concurrent_bag.hpp:149
auto is_read_only() const noexcept -> bool override
Gets a value indicating whether the xtd::collections::concurrent::concurrent_bag <type_t> is read-onl...
Definition concurrent_bag.hpp:88
typename iproducer_consumer_collection< type_t >::value_type value_type
Represents the concurrent bag value type.
Definition concurrent_bag.hpp:43
xtd::collections::generic::queue< value_type > collection_type
Represents the concurrent bag collection type.
Definition concurrent_bag.hpp:49
concurrent_bag(std::initializer_list< type_t > items)
Constructs the container with the contents of the specified initializer list, and allocator.
Definition concurrent_bag.hpp:63
const value_type & const_reference
Represents the const reference of list value type.
Definition concurrent_bag.hpp:47
concurrent_bag(const xtd::collections::generic::ienumerable< value_type > &collection)
Initializes a new instance of the xtd::collections::concurrent::concurrent_bag <type_t> class that co...
Definition concurrent_bag.hpp:68
collection_type * local_storage_type
Represents the concurrent bag collection type.
Definition concurrent_bag.hpp:51
concurrent_bag()=default
Initializes a new instance of the xtd::collections::concurrent::concurrent_bag <type_t> class.
auto to_array() const -> xtd::array< value_type > override
Copies the elements contained in the xtd::collections::concurrent::concurrent_bag <type_t> to a new a...
Definition concurrent_bag.hpp:138
auto try_peek(reference item) const -> bool
Attempts to return an object from the xtd::collections::concurrent::concurrent_bag <type_t> without r...
Definition concurrent_bag.hpp:164
auto copy_to(xtd::array< value_type > &array, xtd::usize array_index) const -> void override
Copies the elements of the xtd::collections::concurrent::concurrent_bag <type_t> to an xtd::array,...
Definition concurrent_bag.hpp:119
auto try_take(reference item) -> bool override
Attempts to remove and return an object from the xtd::collections::concurrent::concurrent <type_t>.
Definition concurrent_bag.hpp:177
value_type & reference
Represents the reference of list value type.
Definition concurrent_bag.hpp:45
Defines methods to manipulate thread-safe collections intended for producer/consumer usage....
Definition iproducer_consumer_collection.hpp:30
virtual auto sync_root() const noexcept -> const object &=0
Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollecti...
virtual auto count() const noexcept -> xtd::usize=0
Gets the number of elements contained in the xtd::collections::generic::icollection.
virtual auto to_array() const -> xtd::array< type_t >=0
Copies the elements contained in the xtd::collections::concurrent::iproducer_consumer_collection <typ...
Represents a collection of keys and values.
Definition dictionary.hpp:63
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:40
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:80
auto add_range(const xtd::collections::generic::ienumerable< type_t > &enumerable) -> void
Adds copy of elements from the specified collection to the end of the xtd::collections::generic::list...
Definition list.hpp:297
Represents a first-in, first-out collection of objects.
Definition queue.hpp:47
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
static auto decrement(int32 &location) noexcept -> int32
Decrements a specified variable and stores the result, as an atomic operation.
static auto increment(int32 &location) noexcept -> int32
Increments a specified variable and stores the result, as an atomic operation.
static auto current_thread() noexcept -> thread &
Gets the currently running thread.
Contains xtd::collections::generic::dictionary <key_t, value_t> class.
Contains xtd::collections::generic::queue <value_t> class.
generic::enumerator< xtd::any_object > enumerator
Supports a simple iteration over a non-generic collection.
Definition enumerator.hpp:28
static auto format(const basic_string< char > &fmt, args_t &&... args) -> basic_string
@ not_supported
The method or operation is not supported.
Definition exception_case.hpp:77
@ argument_out_of_range
The argument is out of range.
Definition exception_case.hpp:35
#define self_
The self_ expression is a reference value expression whose value is the reference of the implicit obj...
Definition self.hpp:20
#define lock_guard_(object)
The lock_guard_ keyword marks a statement block_guard as a critical section by obtaining the mutual-e...
Definition lock_guard.hpp:65
std::int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
std::uint64_t uint64
Represents a 64-bit unsigned integer.
Definition uint64.hpp:23
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
auto as(any_object &o) -> type_t
Casts a type into another type.
Definition __as_any_object.hpp:60
auto new_ptr(args_t &&... args) -> xtd::ptr< type_t >
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
@ i
The I key.
Definition console_key.hpp:104
Contains xtd::threading::interlocked class.
Contains xtd::collections::concurrent::iproducer_consumer_collection <type_t> interface.
Contains lock_guard_ keyword.
Provides several thread-safe collection classes that should be used in place of the corresponding typ...
Definition concurrent_bag.hpp:20
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
The xtd::collections namespace contains interfaces and classes that define various collections of obj...
Definition any_pair.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
const type_t & const_reference
Represents the read_only_span const reference type.
Definition read_only_span.hpp:69
Contains xtd::new_ptr method.