# Poco

# template < typename T >

# class SPSCQueue

Library: Foundation
Package: Core
Header: Poco/SPSCQueue.h

## Description

A lock-free single-producer single-consumer (SPSC) bounded queue.

This queue is optimized for the case where exactly one thread produces items and exactly one thread consumes them. It provides very low latency and high throughput by avoiding locks and minimizing cache line contention.

The queue has a fixed capacity specified at construction time. When full, tryPush() returns false. When empty, tryPop() returns false.

Usage:

    SPSCQueue<Message> queue(1024);

    // Producer thread:
    if (queue.tryPush(msg)) { /* success */ }
    // or with move:
    if (queue.tryPush(std::move(msg))) { /* success */ }

    // Consumer thread:
    Message msg;
    if (queue.tryPop(msg)) { /* got message */ }

Thread safety:

- Exactly one thread may call tryPush/emplace (producer)
- Exactly one thread may call tryPop (consumer)
- size(), empty(), capacity() may be called from any thread

Note: This queue does not provide blocking operations. For blocking behavior, combine with a semaphore or condition variable.

## Member Summary

Member Functions: capacity, emplace, empty, operator =, size, tryPop, tryPush

## Constructors

### SPSCQueue

explicit SPSCQueue(
std::size_t capacity
);

Creates the queue with the given capacity. Capacity must be at least 1 and will be rounded up to the next power of two for efficient indexing.

### SPSCQueue

SPSCQueue(
const SPSCQueue & param368
) = delete;

### SPSCQueue

SPSCQueue(
SPSCQueue && param370
) = delete;

## Destructor

### ~SPSCQueue

~SPSCQueue();

Destroys the queue.

## Member Functions

### capacity

std::size_t capacity() const;

Returns the capacity of the queue.

### emplace

template < typename ... Args > bool emplace(
Args && ... args
);

Attempts to construct an item in-place at the back of the queue. Returns true if successful, false if the queue is full.

### empty

bool empty() const;

Returns true if the queue appears to be empty. This is approximate for the same reason as size().

### operator =

SPSCQueue & operator = (
const SPSCQueue & param369
) = delete;

### operator =

SPSCQueue & operator = (
SPSCQueue && param371
) = delete;

### size

std::size_t size() const;

Returns an approximate count of items in the queue. This is approximate because the producer and consumer may be modifying the queue concurrently.

### tryPop

bool tryPop(
T & item
);

Attempts to pop an item from the queue. If successful, moves the item into the provided reference and returns true. Returns false if the queue is empty.

### tryPush

bool tryPush(
const T & item
);

Attempts to push a copy of item onto the queue. Returns true if successful, false if the queue is full.

### tryPush

bool tryPush(
T && item
);

Attempts to push item onto the queue using move semantics. Returns true if successful, false if the queue is full.

## Variables

### CACHE_LINE_SIZE

static constexpr std::size_t CACHE_LINE_SIZE = std::hardware_destructive_interference_size;