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
74 template<xtd::iterable iterable_t>
75 concurrent_bag(iterable_t&& items) {
76 for (const auto& item : items)
77 add(item);
78 }
79
80
82
86 [[nodiscard]] auto count() const noexcept -> xtd::usize override {return as<xtd::usize>(count_);}
87
90 [[nodiscard]] auto is_empty() const noexcept -> bool {return count_ == 0;}
91
95 [[nodiscard]] auto is_read_only() const noexcept -> bool override {return false;}
96
111 [[nodiscard]] auto is_synchronized() const noexcept -> bool override {return true;}
113
115
121
126 auto copy_to(xtd::array<value_type>& array, xtd::usize array_index) const -> void override {
128 static thread_local auto items = xtd::array<value_type> {};
129 items = to_array();
130 for (auto i = xtd::usize {0}; i < count(); ++i)
131 array[array_index + i] = items[i];
132 }
133
137 [[nodiscard]] auto get_enumerator() const -> xtd::collections::generic::enumerator<value_type> override {
138 static thread_local auto items = xtd::array<value_type> {};
139 items = to_array();
140 return items.get_enumerator();
141 }
142
146 [[nodiscard]] auto to_array() const -> xtd::array<value_type> override {
148 lock_guard_(storages_) {
149 for (const auto& [thread_id, storage] : storages_)
150 result.add_range(*storage);
151 }
152 return result.to_array();
153 }
154
157 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {return xtd::string::format("[{}]", xtd::string::join(", ", self_));}
158
163 auto try_add(const_reference item) -> bool override {
164 current_local_storage()->enqueue(item);
166 return true;
167 }
168
172 auto try_peek(reference item) const -> bool {
173 if (current_local_storage()->count()) return current_local_storage()->try_peek(item);
174 lock_guard_(storages_) {
175 for (auto& [thread_id, storage] : storages_) {
176 if (storage->count()) return storage->try_peek(item);
177 }
178 }
179 return false;
180 }
181
185 auto try_take(reference item) -> bool override {
186 if (current_local_storage()->count()) {
187 auto result = current_local_storage()->try_dequeue(item);
188 if (result) {
190 return true;
191 }
192 }
193 lock_guard_(storages_) {
194 for (auto& [thread_id, storage] : storages_) {
195 if (storage->count()) {
196 auto result = storage->try_dequeue(item);
197 if (result) {
199 return true;
200 }
201 }
202 }
203 }
204 return false;
205 }
206
207 private:
209 [[nodiscard]] auto contains(const_reference item) const noexcept -> bool override {return false;}
210 auto remove(const_reference item) -> bool override {return false;}
211 [[nodiscard]] auto sync_root() const noexcept -> const object& override {return storages_;}
212
213 auto current_local_storage() const -> local_storage_type {
214 if (thread_local_storages_.contains_key(instance_id_)) return thread_local_storages_[instance_id_];
215 lock_guard_(storages_) {
216 auto id = xtd::threading::thread::current_thread().thread_id();
217 if (!storages_.contains_key(id)) storages_.add(id, new_ptr<collection_type>());
218 auto storage = storages_[id].get();
219 thread_local_storages_[instance_id_] = storage;
220 return storage;
221 }
222 return thread_local_storages_[instance_id_];
223 }
224
225 inline static thread_local xtd::collections::generic::dictionary<xtd::uint64, local_storage_type> thread_local_storages_;
226 inline static std::atomic<xtd::uint64> next_id_;
227 mutable storage_type storages_;
228 xtd::int64 count_ = 0;
229 xtd::uint64 instance_id_ = ++next_id_;
230 };
231
233 // Deduction guides for xtd::collections::concurrent::concurrent_bag
234 // {
235 template<typename type_t>
236 concurrent_bag(std::initializer_list<type_t>) -> concurrent_bag<type_t>;
237
238 template<typename type_t>
239 concurrent_bag(const xtd::collections::generic::ienumerable<type_t>&) -> concurrent_bag<type_t>;
240
241 template<xtd::iterable iterable_t>
243 // }
245 }
246 }
247}
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:1259
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:111
auto get_enumerator() const -> xtd::collections::generic::enumerator< value_type > override
Returns an enumerator that iterates through a collection.
Definition concurrent_bag.hpp:137
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:86
auto add(const_reference item) -> void override
Adds an item to the xtd::collections::concurrent::concurrent_bag <type_t>.
Definition concurrent_bag.hpp:120
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:163
concurrent_bag(iterable_t &&items)
Initializes a new instance of the xtd::iterable that contains elements copied from the specified coll...
Definition concurrent_bag.hpp:75
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:90
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current object.
Definition concurrent_bag.hpp:157
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:95
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:146
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:172
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:126
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:185
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
virtual auto clear() -> void=0
Removes all items from the xtd::collections::generic::icollection <type_t>.
typename xtd::collections::generic::ienumerable< type_t >::value_type value_type
Represents the xtd::collections::generic::icollection value type.
Definition icollection.hpp:51
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:82
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:315
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:40
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:72
Contains xtd::new_ptr method.