https://github.com/soenneker/soenneker.utils.concurrentcircularqueue
A thread-safe collection type for a fixed length of elements, overwriting the oldest element
https://github.com/soenneker/soenneker.utils.concurrentcircularqueue
buffer circular collection concurrent concurrentcircularqueue csharp dotnet queue thread-safe util utils
Last synced: 4 months ago
JSON representation
A thread-safe collection type for a fixed length of elements, overwriting the oldest element
- Host: GitHub
- URL: https://github.com/soenneker/soenneker.utils.concurrentcircularqueue
- Owner: soenneker
- License: mit
- Created: 2024-03-02T23:35:56.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2026-01-15T20:02:41.000Z (6 months ago)
- Last Synced: 2026-01-16T09:58:20.918Z (6 months ago)
- Topics: buffer, circular, collection, concurrent, concurrentcircularqueue, csharp, dotnet, queue, thread-safe, util, utils
- Language: C#
- Homepage: https://soenneker.com
- Size: 124 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
[](https://www.nuget.org/packages/soenneker.utils.concurrentcircularqueue/)
[](https://github.com/soenneker/soenneker.utils.concurrentcircularqueue/actions/workflows/publish-package.yml)
[](https://www.nuget.org/packages/soenneker.utils.concurrentcircularqueue/)
[](https://github.com/soenneker/soenneker.utils.concurrentcircularqueue/actions/workflows/codeql.yml)
#  Soenneker.Utils.ConcurrentCircularQueue
### A thread-safe collection type for a fixed length of elements, overwriting the oldest element
## Installation
```
dotnet add package Soenneker.Utils.ConcurrentCircularQueue
```
## Usage
### Creating an Instance
Instantiate a `ConcurrentCircularQueue` object by specifying the maximum size of the queue. Optionally, asynchronous locking is available for perfect `.Contains().`
```csharp
// Creates a queue with a maximum size of 3.
var myQueue = new ConcurrentCircularQueue(3, locking: false);
```
### Enqueueing Items
Add an item to the queue. If the queue has reached its maximum size, the oldest item will be removed.
```csharp
await myQueue.Enqueue(1);
await myQueue.Enqueue(2);
await myQueue.Enqueue(3);
await myQueue.Enqueue(4);
// The queue now contains 2, 3, and 4.
```
### Dequeueing Items
Remove and return the oldest item from the queue.
```csharp
(bool success, int result) = await myQueue.TryDequeue();
```
### Checking If an Item Exists
Determine if a specific item is in the queue.
```csharp
bool exists = await myQueue.Contains(item);
```
### Count
Retrieve the current number of items in the queue.
```csharp
int currentCount = await myQueue.Count();
```