Skip to main content

BlockingQueue

BlockingQueue

A thread-safe, blocking queue for producer-consumer patterns. More…

#include <producer_consumer_pipeline.hpp>

Public Functions

Name
BlockingQueue(size_t max_size)
Constructs a blocking queue with a specified maximum size.
bool push(T && item)
Pushes an item into the queue, blocking if the queue is full.
bool pop(T & item)
Pops an item from the queue, blocking if the queue is empty.
void close()
Closes the queue, preventing any further pushes.

Detailed Description

template <typename T >
class BlockingQueue;

A thread-safe, blocking queue for producer-consumer patterns.

Template Parameters:

  • T The type of items to be stored in the queue.

This queue has a maximum size to prevent excessive memory usage. It is designed to be closed to signal the end of production, allowing consumers to terminate gracefully.

Public Functions Documentation

function BlockingQueue

inline explicit BlockingQueue(
    size_t max_size
)

Constructs a blocking queue with a specified maximum size.

Parameters:

  • max_size The maximum number of items the queue can hold.

function push

inline bool push(
    T && item
)

Pushes an item into the queue, blocking if the queue is full.

Parameters:

  • item The item to push, moved into the queue.

Return: True if the item was pushed successfully, false if the queue was closed.

function pop

inline bool pop(
    T & item
)

Pops an item from the queue, blocking if the queue is empty.

Parameters:

  • item A reference to store the popped item.

Return: True if an item was popped successfully, false if the queue is empty and has been closed.

function close

inline void close()

Closes the queue, preventing any further pushes.

This will unblock any waiting producer or consumer threads.


Updated on 2026-11-13 at 20:25:41 +0000