You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+47-26Lines changed: 47 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -94,7 +94,7 @@ When the timed wait expires with an empty queue, the loop simply continues back
94
94
95
95
**3. Dequeue and back pressure release**
96
96
97
-
Messages are dequeued with `m_queue.top()` / `m_queue.pop()`, which returns the highest-priority waiting message (`HIGH` before `NORMAL` before `LOW`). After removing a message, `m_cvNotFull` is signalled to wake any producer that blocked in `PostMsg()` because the queue was at its `MAX_QUEUE_SIZE` limit.
97
+
Messages are dequeued from the first non-empty priority queue, checked in order `HIGH`, `NORMAL`, `LOW`. Each queue is a `std::deque` drained front-to-back, so messages posted at the same priority are always processed in the order they were posted. After removing a message, `m_cvNotFull` is signalled to wake any producer that blocked in `PostMsg()` because the queue was at its `MAX_QUEUE_SIZE` limit.
Messages are stored in a `std::priority_queue` rather than a plain `std::queue`. A custom comparator ensures messages with a higher `Priority` value are dequeued first.
226
+
Messages are stored in one `std::deque` per priority level rather than a single combined queue:
200
227
201
228
```cpp
202
229
enum class Priority { LOW = 0, NORMAL = 1, HIGH = 2 };
203
230
204
-
struct ThreadMsgComparator {
205
-
bool operator()(const std::shared_ptr<ThreadMsg>& a,
When several messages are in the queue simultaneously, the worker thread always processes the `HIGH` priority message first, then `NORMAL`, then `LOW`, regardless of the order they were posted. This is useful for giving urgent work — such as a shutdown or error signal — preferential access to the thread without a separate fast-path queue.
236
+
`PostMsg()` appends to the deque matching the message's priority; `Process()` dequeues from the first non-empty deque, checked in order `HIGH`, `NORMAL`, `LOW`. When several messages are queued simultaneously, the worker thread always processes `HIGH` priority messages first, then `NORMAL`, then `LOW`, regardless of post order across levels — useful for giving urgent work, such as a shutdown or error signal, preferential access to the thread without a separate fast-path queue. Because each level is its own FIFO deque, messages posted at the *same* priority are always processed in the order they were posted.
237
+
238
+
> An earlier version of this class used a single `std::priority_queue` with a comparator over `Priority`. That correctly ordered different priorities, but `std::priority_queue`'s underlying binary heap does not preserve insertion order among equal elements — messages posted at the same priority could be dequeued out of order. The per-priority deque design fixes this while keeping the same `HIGH` > `NORMAL` > `LOW` ordering guarantee across levels.
0 commit comments