xtd 1.0.0
Loading...
Searching...
No Matches
xtd::collections::concurrent::iproducer_consumer_collection< type_t > Class Template Referenceabstract
Inheritance diagram for xtd::collections::concurrent::iproducer_consumer_collection< type_t >:
xtd::collections::concurrent::concurrent_bag< type_t >

Definition

template<typename type_t>
class xtd::collections::concurrent::iproducer_consumer_collection< type_t >

Defines methods to manipulate thread-safe collections intended for producer/consumer usage. This interface provides a unified representation for producer/consumer collections so that higher level abstractions such as xtd::collections::concurrent::blocking_collection <type_t> can use the collection as the underlying storage mechanism.

Definition
template<typename type_t>
class iproducer_consumer_collection : public xtd::collections::generic::icollection <type_t>
Defines methods to manipulate thread-safe collections intended for producer/consumer usage....
Definition iproducer_consumer_collection.hpp:30
Header
#include <xtd/collections/concurrent/iproducer_consumer_collection>
Namespace
xtd::collections::concurrent
Library
xtd.core

Public Properties

virtual auto count () const noexcept -> xtd::usize=0
 Gets the number of elements contained in the xtd::collections::generic::icollection.
virtual auto is_synchronized () const noexcept -> bool=0
 Gets a value indicating whether access to the xtd::collections::generic::icollection is synchronized (thread safe).
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::icollection <type_t>.

Public Methods

virtual auto copy_to (xtd::array< type_t > &array, xtd::usize array_index) const -> void=0
 Copies the elements of the xtd::collections::generic::icollection <type_t> to an xtd::array, starting at a particular xtd::array index.
virtual auto get_enumerator () const -> xtd::collections::generic::enumerator< type_t >=0
 Returns an enumerator that iterates through a collection.
virtual auto to_array () const -> xtd::array< type_t >=0
 Copies the elements contained in the xtd::collections::concurrent::iproducer_consumer_collection <type_t> to a new array.
virtual auto try_add (const type_t &item) -> bool=0
 Attempts to add an object to the xtd::collections::concurrent::iproducer_consumer_collection <type_t>.
virtual auto try_take (type_t &item) -> bool=0
 Attempts to remove and return an object from the xtd::collections::concurrent::iproducer_consumer_collection <type_t>.

Member Function Documentation

◆ count()

template<typename type_t>
virtual auto xtd::collections::concurrent::iproducer_consumer_collection< type_t >::count ( ) const -> xtd::usize
nodiscardpure virtualnoexcept

Gets the number of elements contained in the xtd::collections::generic::icollection.

Returns
The number of elements contained in the xtd::collections::generic::icollection.

Implemented in xtd::collections::concurrent::concurrent_bag< type_t >.

◆ is_synchronized()

template<typename type_t>
virtual auto xtd::collections::concurrent::iproducer_consumer_collection< type_t >::is_synchronized ( ) const -> bool
nodiscardpure virtualnoexcept

Gets a value indicating whether access to the xtd::collections::generic::icollection is synchronized (thread safe).

Returns
true if access to the ICollection is synchronized (thread safe); otherwise, false.
Remarks
xtd::collections::concurrent::iproducer_consumer_collection::sync_root returns an object, which can be used to synchronize access to the xtd::collections::generic::icollection.
Most collection classes in the xtd::collections namespace also implement a Synchronized method, which provides a synchronized wrapper around the underlying collection.
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
The following code example shows how to lock the collection using the xtd::collections::concurrent::iproducer_consumer_collection::sync_root property during the entire enumeration.
icollection& my_collection = some_collection;
lock_(my_collection.sync_root()) {
for (const auto& item : my_collection) {
// Insert your code here.
}
}
generic::icollection< xtd::any_object > icollection
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition icollection.hpp:32
#define lock_(object)
The lock_ keyword marks a statement block as a critical section by obtaining the mutual-exclusion loc...
Definition lock.hpp:68

Implemented in xtd::collections::concurrent::concurrent_bag< type_t >.

◆ sync_root()

template<typename type_t>
virtual auto xtd::collections::concurrent::iproducer_consumer_collection< type_t >::sync_root ( ) const -> const object &
nodiscardpure virtualnoexcept

Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollection <type_t>.

Returns
An object that can be used to synchronize access to the the xtd::collections::generic::icollection <type_t>.
Remarks
For collections whose underlying store is not publicly available, the expected implementation is to return the current instance. Note that the pointer to the current instance might not be sufficient for collections that wrap other collections; those should return the underlying collection's sync_root property.
Most collection classes in the xts::.collections namespace also implement a synchronized method, which provides a synchronized wrapper around the underlying collection. However, derived classes can provide their own synchronized version of the collection using the xtd::collections::generic::icollection::sync_root property. The synchronizing code must perform operations on the xtd::collections::generic::icollection::sync_root property of the collection, not directly on the collection. This ensures proper operation of collections that are derived from other objects. Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the collection instance.
In the absence of a synchronized method on a collection, the expected usage for the xtd::collections::generic::icollection::sync_root looks as follows:
icollection& my_collection = some_collection;
lock_(my_collection.sync_root()) {
// Some operation on the collection, which is now thread safe.
}
@encode
@remarks Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
@remarks The following code example shows how to lock the collection using the xtd::collections::generic::icollection::sync_root property during the entire enumeration.
@code
icollection& my_collection = some_collection;
lock_(my_collection.sync_root()) {
for (const auto& item : my_collection) {
// Insert your code here.
}
}
Defines the base class for predefined exceptions in the xtd namespace.
Definition exception.hpp:29
Definition enumeration.hpp:12
generic::enumerator< xtd::any_object > enumerator
Supports a simple iteration over a non-generic collection.
Definition enumerator.hpp:28
auto is(xtd::any value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:485
@ other
The operating system is other.
Definition platform_id.hpp:60
@ a
The A key.
Definition console_key.hpp:88

◆ copy_to()

template<typename type_t>
virtual auto xtd::collections::concurrent::iproducer_consumer_collection< type_t >::copy_to ( xtd::array< type_t > & array,
xtd::usize array_index ) const -> void
pure virtual

Copies the elements of the xtd::collections::generic::icollection <type_t> to an xtd::array, starting at a particular xtd::array index.

Parameters
arrayThe one-dimensional xtd::array that is the destination of the elements copied from xtd::collections::generic::icollection <type_t>. The xtd::array must have zero-based indexing.
array_indexThe zero-based index in array at which copying begins.
Exceptions
xtd::argument_exceptionThe number of elements in the source xtd::collections::generic::icollection <type_t> is greater than the available space from `array_index` to the end of the destination `array`.

◆ get_enumerator()

template<typename type_t>
virtual auto xtd::collections::concurrent::iproducer_consumer_collection< type_t >::get_enumerator ( ) const -> xtd::collections::generic::enumerator< type_t >
nodiscardpure virtual

Returns an enumerator that iterates through a collection.

Returns
An xtd::collections::generic::enumerator object that can be used to iterate through the collection.

Implemented in xtd::collections::concurrent::concurrent_bag< type_t >.

◆ to_array()

template<typename type_t>
virtual auto xtd::collections::concurrent::iproducer_consumer_collection< type_t >::to_array ( ) const -> xtd::array< type_t >
nodiscardpure virtual

Copies the elements contained in the xtd::collections::concurrent::iproducer_consumer_collection <type_t> to a new array.

Returns
A new array containing the elements copied from the xtd::collections::concurrent::iproducer_consumer_collection <type_t.
Remarks
The method provides a snapshot of the underlying collection. It is possible for other threads to add or remove items immediately after the array is made.

Implemented in xtd::collections::concurrent::concurrent_bag< type_t >.

◆ try_add()

template<typename type_t>
virtual auto xtd::collections::concurrent::iproducer_consumer_collection< type_t >::try_add ( const type_t & item) -> bool
pure virtual

Attempts to add an object to the xtd::collections::concurrent::iproducer_consumer_collection <type_t>.

Parameters
itemThe object to add to the xtd::collections::concurrent::iproducer_consumer_collection <type_t>.
Returns
true if the object was added successfully; otherwise, false. @exceprion xtd::argument_exception The item was invalid for this collection.

◆ try_take()

template<typename type_t>
virtual auto xtd::collections::concurrent::iproducer_consumer_collection< type_t >::try_take ( type_t & item) -> bool
pure virtual

Attempts to remove and return an object from the xtd::collections::concurrent::iproducer_consumer_collection <type_t>.

Parameters
itemWhen this method returns, if the object was removed and returned successfully, item contains the removed object. If no object was available to be removed, the value is unspecified.
Returns
true if an object was removed and returned successfully; otherwise, false.

The documentation for this class was generated from the following file: