VR-Engage  2.2
Loading...
Searching...
No Matches
locklessQueue.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4*******************************************************************************/
5
6//! \file locklessQueue.h
7//! \brief Defines a thread-safe queue for single reader/single writer scenarios
8//! \ingroup vreRadarFxShared
9
10#pragma once
11
12#include <vector>
13
14namespace makRadarFx
15{
16//! \brief Thread-safe queue for single reader/single writer communication
17//! \tparam T Type of elements stored in the queue
18//!
19//! This template class provides a thread-safe queue designed for transferring data
20//! between a single producer thread and a single consumer thread without requiring
21//! locks. The implementation may block if the queue fills up, providing
22//! backpressure to prevent overflow. This queue is optimized for performance in
23//! inter-thread communication scenarios.
24template <class T>
26{
27public:
28 //! \brief Constructor with specified queue capacity
29 //! \param size Maximum number of elements that can be stored in the queue
30 //!
31 //! Creates a new lockless queue with the specified capacity
32 DtLocklessQueue(int size);
33
34 //! \brief Destructor
35 //!
36 //! Cleans up and releases all queue resources
38
39 //! \brief Adds an element to the queue
40 //! \param newElement Element to add to the queue
41 //!
42 //! Adds an element to the queue. If the queue is full, this method will
43 //! block until space becomes available. This method is safe to call
44 //! from a single producer thread.
45 void push(T newElement);
46
47 //! \brief Removes and returns an element from the queue
48 //! \return The next element from the queue
49 //!
50 //! Removes and returns the next element from the queue. If the queue is empty,
51 //! this method will block until data becomes available. This method is safe
52 //! to call from a single consumer thread.
53 T pop();
54
55 //! \brief Checks if there is room to add an element without blocking
56 //! \return True if push() can be called without blocking, false otherwise
57 //!
58 //! This method checks if the queue has space available for adding an element.
59 //! If this returns false, a call to push() will block until space is available.
60 bool okToPush();
61
62 //! \brief Gets the current number of elements in the queue
63 //! \return Number of elements currently in the queue
64 //!
65 //! Returns the number of elements currently stored in the queue.
66 //! This is thread-safe but may not be atomically consistent with other operations.
67 int count();
68
69private:
70 //! \brief Default constructor
71 //!
72 //! Intentionally unimplemented to prevent usage
74
75 //! \brief Copy constructor
76 //! \param orig Queue to copy from
77 //!
78 //! Intentionally unimplemented to prevent copying
80
81 //! \brief Assignment operator
82 //! \param rhs Queue to assign from
83 //! \return Reference to this queue
84 //!
85 //! Intentionally unimplemented to prevent assignment
87
88protected:
89 //! \brief Storage for queue elements
90 std::vector<T> myElements;
91
92 //! \brief Index of the next element to read
93 //! Value of -1 indicates the queue is empty
95
96 //! \brief Index where the next element will be written
98};
99} // namespace makRadarFx
100
101#define DtLocklessQueue_INL_
102#include "locklessQueue.inl"
103#undef DtLocklessQueue_INL_
int myWriteHead
Index where the next element will be written.
Definition locklessQueue.h:97
DtLocklessQueue(int size)
Constructor with specified queue capacity.
DtLocklessQueue & operator=(const DtLocklessQueue &rhs)
Assignment operator.
std::vector< T > myElements
Storage for queue elements.
Definition locklessQueue.h:90
T pop()
Removes and returns an element from the queue.
DtLocklessQueue()
Default constructor.
int myReadHead
Index of the next element to read Value of -1 indicates the queue is empty.
Definition locklessQueue.h:94
bool okToPush()
Checks if there is room to add an element without blocking.
int count()
Gets the current number of elements in the queue.
DtLocklessQueue(const DtLocklessQueue &orig)
Copy constructor.
void push(T newElement)
Adds an element to the queue.
Definition radarFxConnectorDriver.h:21