Class ThreadSafeCollection<T>
Represents a thread-safe collection of objects that can be accessed by multiple threads concurrently.
Namespace: Coree.NETStandard.Classes
Assembly: Coree.NETStandard.dll
Syntax
public class ThreadSafeCollection<T> : IEnumerable<T?>, IEnumerable where T : class?
Type Parameters
Name | Description |
---|---|
T | The type of elements in the collection. This type can be a class, including nullable reference types. |
Remarks
This collection uses locking to ensure that its operations are thread-safe. The collection is implemented
as a list where items can be added, removed, or retrieved in a manner that prevents race conditions
and data corruption when accessed from multiple threads. Null values can be stored, depending on the type T
.
Constructors
| Edit this page View SourceThreadSafeCollection()
Initializes a new instance of the ThreadSafeCollection class that is empty.
Declaration
public ThreadSafeCollection()
Remarks
Creates an empty collection with no items. The collection is ready to be accessed by multiple threads
using its thread-safe methods. It supports storing null values, if the type T
permits.
Properties
| Edit this page View SourceCount
Gets the number of items in the collection, ensuring thread safety.
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
int | The number of items currently in the collection. Access is synchronized to prevent race conditions. |
HasItems
Gets a value indicating whether the collection contains any items, ensuring thread safety.
Declaration
public bool HasItems { get; }
Property Value
Type | Description |
---|---|
bool |
|
Remarks
This property locks the collection to ensure that the check is performed in a thread-safe manner, preventing race conditions that could arise from concurrent modifications to the collection.
this[int]
Gets or sets the item at the specified index in a thread-safe manner.
Declaration
public T? this[int index] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
int | index | The zero-based index of the item to get or set. |
Property Value
Type | Description |
---|---|
T | The item at the specified index. |
Remarks
Access to the item is synchronized to ensure thread safety. Setting an item acquires a lock to prevent race conditions.
Exceptions
Type | Condition |
---|---|
Argument |
Thrown when the index is outside the bounds of the collection. |
Methods
| Edit this page View SourceAdd(T?)
Adds an item to the collection in a thread-safe manner.
Declaration
public void Add(T? item)
Parameters
Type | Name | Description |
---|---|---|
T | item | The item to be added to the collection. Can be null if the type |
Remarks
Synchronizes access to the collection to safely add the item, supporting concurrent modifications.
Dequeue()
Removes and returns the item at the beginning of the collection in a thread-safe manner.
Declaration
public T? Dequeue()
Returns
Type | Description |
---|---|
T | The item at the beginning of the collection if it exists; otherwise, throws an InvalidOperationException. |
Remarks
This method locks the collection to ensure thread safety during the remove operation. It is part of the thread-safe queue functionality, providing a way to dequeue items (remove from the beginning). If the collection is empty, it throws an InvalidOperationException to enforce handling of the empty state condition by the caller.
Exceptions
Type | Condition |
---|---|
Invalid |
Thrown when attempting to perform the Dequeue operation on an empty collection. |
Enqueue(T?)
Adds an item to the end of the collection in a thread-safe manner.
Declaration
public void Enqueue(T? item)
Parameters
Type | Name | Description |
---|---|---|
T | item | The item to add to the collection. Can be null if the type |
Remarks
This method ensures that items can be added to the collection safely from multiple threads by synchronizing access. It functions as part of the thread-safe queue functionality, allowing items to be enqueued (added to the end).
GetCollectionCopyToList()
Creates and returns a deep copy of the entire collection as a List of T.
Declaration
public List<T?> GetCollectionCopyToList()
Returns
Type | Description |
---|---|
List<T> | A new List containing deep copies of the items in the collection. Returns an empty list if the collection is empty. |
Remarks
Locks the collection to ensure thread safety during the copy process. This method uses JSON serialization to create deep copies of the items, providing isolation from the original items. Modifications to the returned list or its items will not affect the original collection.
GetEnumerator()
Returns an enumerator that iterates through the collection in a thread-safe manner.
Declaration
public IEnumerator<T?> GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator<T> | An IEnumerator<T?> that can be used to iterate through the collection. |
Remarks
This method creates a snapshot of the current state of the collection and returns an enumerator for this snapshot, allowing for safe iteration over the collection items even when other threads might be modifying the collection concurrently. Note that the snapshot is a shallow copy; thus, the enumeration reflects the collection's state at the moment of the snapshot's creation.
GetItemAt(int)
Retrieves the item at the specified index in a thread-safe manner.
Declaration
public T? GetItemAt(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | The zero-based index of the item to retrieve. |
Returns
Type | Description |
---|---|
T | The item at the specified index. Can be null if the item itself is null or if the type |
Remarks
Accesses the collection in a synchronized context to ensure safe retrieval of the item.
Exceptions
Type | Condition |
---|---|
Argument |
Thrown when the specified index is outside the bounds of the collection. |
GetItemCopyAt(int)
Returns a deep copy of the item at the specified index, ensuring thread safety and isolation from the original collection.
Declaration
public T? GetItemCopyAt(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | The zero-based index of the item to copy. |
Returns
Type | Description |
---|---|
T | A deep copy of the item at the specified index. Returns null if the original item is null. |
Remarks
This method locks the collection during the operation and uses JSON serialization to create a deep copy of the item,
ensuring that modifications to the returned object do not affect the original item in the collection.
Throws Argument
Exceptions
Type | Condition |
---|---|
Argument |
Thrown when the index is out of range. |
LockedLinqDeepCopy<TResult>(Func<IEnumerable<T?>, IEnumerable<TResult>>)
Performs a LINQ operation on a snapshot of the collection and returns a deep copy of the results in a thread-safe manner.
Declaration
public IEnumerable<TResult> LockedLinqDeepCopy<TResult>(Func<IEnumerable<T?>, IEnumerable<TResult>> operation)
Parameters
Type | Name | Description |
---|---|---|
Func<IEnumerable<T>, IEnumerable<TResult>> | operation | A function representing the LINQ operation to be performed on the collection. |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | An IEnumerable<T> that contains a deep copy of the result of applying the LINQ operation on the collection snapshot. |
Type Parameters
Name | Description |
---|---|
TResult | The type of the result elements. |
Remarks
Similar to Locked
LockedLinq<TResult>(Func<IEnumerable<T?>, IEnumerable<TResult>>)
Performs a LINQ operation on a snapshot of the collection in a thread-safe manner.
Declaration
public IEnumerable<TResult> LockedLinq<TResult>(Func<IEnumerable<T?>, IEnumerable<TResult>> operation)
Parameters
Type | Name | Description |
---|---|---|
Func<IEnumerable<T>, IEnumerable<TResult>> | operation | A function representing the LINQ operation to be performed on the collection. |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | An IEnumerable<T> that contains the result of applying the LINQ operation on the collection snapshot. |
Type Parameters
Name | Description |
---|---|
TResult | The type of the result elements. |
Remarks
This method locks the collection, takes a snapshot, and then applies the specified LINQ operation. It ensures thread safety by preventing other operations from modifying the collection during execution. The operation is performed on a snapshot to avoid locking during the entire enumeration, but it means the operation does not reflect changes made to the collection after the snapshot is taken.
Peek()
Returns the last item from the collection without removing it, in a thread-safe manner.
Declaration
public T? Peek()
Returns
Type | Description |
---|---|
T | The last item of the collection if it exists; otherwise, throws an InvalidOperationException. |
Remarks
This method locks the collection to ensure thread safety during the retrieval operation. If the collection is empty, it throws an InvalidOperationException to notify the caller of the empty state, aligning with the behavior of Pop.
Exceptions
Type | Condition |
---|---|
Invalid |
Thrown when the collection is empty. |
Pop()
Removes and returns the last item from the collection in a thread-safe manner.
Declaration
public T? Pop()
Returns
Type | Description |
---|---|
T | The last item of the collection if it exists; otherwise, throws an InvalidOperationException. |
Remarks
This method locks the collection to ensure thread safety during the remove operation. If the collection is empty, it throws an InvalidOperationException. This change ensures that the caller is explicitly aware of the empty state.
Exceptions
Type | Condition |
---|---|
Invalid |
Thrown when the collection is empty. |
Push(T?)
Adds an item to the collection in a thread-safe manner.
Declaration
public void Push(T? item)
Parameters
Type | Name | Description |
---|---|---|
T | item | The item to add to the collection. Can be null for nullable reference types. |
Remarks
This method ensures that the collection can be safely modified from multiple threads by synchronizing access. The item is added to the end of the collection. It accepts null values for types that allow it.
Remove(T?)
Removes the first occurrence of a specific item from the collection in a thread-safe manner.
Declaration
public void Remove(T? item)
Parameters
Type | Name | Description |
---|---|---|
T | item | The item to remove from the collection. Can be null if the type |
Remarks
Synchronizes access to the collection to safely remove the item, supporting concurrent modifications. If the item is not found, no action is taken.
Take(Func<T?, bool>)
Finds and removes the first item matching the given predicate from the collection in a thread-safe manner.
Declaration
public T? Take(Func<T?, bool> predicate)
Parameters
Returns
Type | Description |
---|---|
T | The item that was removed from the collection, or |
Remarks
Synchronizes access to the collection to safely remove the item, supporting concurrent modifications.
If no item matches the predicate, null
is returned.
TakeAll(Func<T?, bool>)
Finds and removes all items matching the given predicate from the collection in a thread-safe manner.
Declaration
public IEnumerable<T?> TakeAll(Func<T?, bool> predicate)
Parameters
Returns
Type | Description |
---|---|
IEnumerable<T> | An enumerable of all items that were removed from the collection. |
Remarks
Synchronizes access to the collection to safely remove the items, supporting concurrent modifications. Returns an empty enumerable if no items match the predicate.
TakeAt(int)
Gets and removes the item at the specified index from the collection in a thread-safe manner.
Declaration
public T? TakeAt(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | The zero-based index of the item to fetch and remove. |
Returns
Type | Description |
---|---|
T | The item that was removed from the collection. |
Remarks
Synchronizes access to the collection to safely remove the item, supporting concurrent modifications.
If the index is out of range, an Argument
Exceptions
Type | Condition |
---|---|
Argument |
Thrown when the index is out of range. |