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
130 [[nodiscard]] auto get_enumerator() const -> xtd::collections::generic::enumerator<value_type> override {
131 static thread_local auto items = xtd::array<value_type> {};
132 items = to_array();
133 return items.get_enumerator();
134 }
135
139 [[nodiscard]] auto to_array() const -> xtd::array<value_type> override {
141 lock_guard_(storages_) {
142 for (const auto& [thread_id, storage] : storages_)
143 result.add_range(*storage);
144 }
145 return result.to_array();
146 }
147
150 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {return xtd::string::format("[{}]", xtd::string::join(", ", self_));}
151
156 auto try_add(const_reference item) -> bool override {
157 current_local_storage()->enqueue(item);
159 return true;
160 }
161
165 auto try_peek(reference item) const -> bool {
166 if (current_local_storage()->count()) return current_local_storage()->try_peek(item);
167 lock_guard_(storages_) {
168 for (auto& [thread_id, storage] : storages_) {
169 if (storage->count()) return storage->try_peek(item);
170 }
171 }
172 return false;
173 }
174
178 auto try_take(reference item) -> bool override {
179 if (current_local_storage()->count()) {
180 auto result = current_local_storage()->try_dequeue(item);
181 if (result) {
183 return true;
184 }
185 }
186 lock_guard_(storages_) {
187 for (auto& [thread_id, storage] : storages_) {
188 if (storage->count()) {
189 auto result = storage->try_dequeue(item);
190 if (result) {
192 return true;
193 }
194 }
195 }
196 }
197 return false;
198 }
199
200 private:
202 [[nodiscard]] auto contains(const_reference item) const noexcept -> bool override {return false;}
203 auto remove(const_reference item) -> bool override {return false;}
204 [[nodiscard]] auto sync_root() const noexcept -> const object& override {return storages_;}
205
206 auto current_local_storage() const -> local_storage_type {
207 if (thread_local_storages_.contains_key(instance_id_)) return thread_local_storages_[instance_id_];
208 lock_guard_(storages_) {
209 auto id = xtd::threading::thread::current_thread().thread_id();
210 if (!storages_.contains_key(id)) storages_.add(id, new_ptr<collection_type>());
211 auto storage = storages_[id].get();
212 thread_local_storages_[instance_id_] = storage;
213 return storage;
214 }
215 return thread_local_storages_[instance_id_];
216 }
217
218 inline static thread_local xtd::collections::generic::dictionary<xtd::uint64, local_storage_type> thread_local_storages_;
219 inline static std::atomic<xtd::uint64> next_id_;
220 mutable storage_type storages_;
221 xtd::int64 count_ = 0;
222 xtd::uint64 instance_id_ = ++next_id_;
223 };
224
226 // Deduction guides for xtd::collections::concurrent::concurrent_bag
227 // {
228 template<typename type_t>
229 concurrent_bag(std::initializer_list<type_t>) -> concurrent_bag<type_t>;
230
231 template<typename type_t>
232 concurrent_bag(const xtd::collections::generic::ienumerable<type_t>&) -> concurrent_bag<type_t>;
233 // }
235 }
236 }
237}
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:130
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:156
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:150
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:139
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:165
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:178
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:302
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:68
Contains xtd::new_ptr method.