TAtomicArray
Overview
TAtomicArray is a template class designed to encapsulate an array (TArray) with thread safety. It uses a critical section (FCriticalSection) to ensure that insertions, pops, and other modifications are performed atomically. This makes it suitable for use in multi-threaded environments where concurrent access to the array is possible.
Properties
Type | Name | Access | Description |
|---|---|---|---|
| ArrayLock | Protected | A critical section used to synchronize access to the underlying array. |
Functions
Return Type | Signature | Description |
|---|---|---|
|
| Inserts an element into the array atomically and returns its index. |
|
| Inserts a moveable element into the array atomically and returns its index. |
|
| Removes and returns the element at the specified index, ensuring atomicity. |
|
| Shrinks the array to remove any invalid elements, ensuring atomicity. |
|
| Returns a copy of the array, ensuring atomicity during the copy operation. |
Usage & Implementation Notes
The class uses
FCriticalSectionfor synchronization, which ensures that all operations are thread-safe.The
Insertmethods provide both lvalue and rvalue references to allow flexibility in how elements are added to the array.The
PopAtmethod safely removes an element by first copying it and then removing it from the array.The
RemoveInvalidmethod is used to optimize memory usage by removing any unused space in the array.