EventBus\
Generic lock-free fan-out bus delivering Event
objects to multiple subscribers,
with compile-time Policy (SyncPolicy
/AsyncPolicy
) and configurable
QueueSize
(default config::DEFAULT_EVENTBUS_QUEUE_SIZE
).
template <typename Event,
typename Policy,
size_t QueueSize = config::DEFAULT_EVENTBUS_QUEUE_SIZE>
class EventBus;
Highlights
Aspect | Details |
---|---|
Queue type | SPSCQueue<QueueItem, QueueSize> per subscriber (lock-free). |
Subscriber modes | PUSH (own thread) or PULL (caller polls queue via getQueue ). |
Sync vs Async | Sync adds TickBarrier* and waits; async enqueues and returns immediately. |
Tick numbering | _tickCounter auto-fills event.tickSequence if present. |
Drain-on-stop | Call enableDrainOnStop() → bus finishes remaining items before exit. |
Public API
```cpp
void subscribe(Listener);
void start(); // spin up PUSH threads
void stop(); // signal shutdown, join threads
void publish(Event); // broadcast to every queue
auto getQueue(SubscriberId)
-> std::optional
Internal Flow
publish()
stamps sequence, buildsQueueItem
viaPolicy::makeItem()
.- Each subscriber’s queue receives the item; in sync mode a local
TickBarrier
enforces deterministic completion. - PUSH listeners loop on
try_pop()
→Policy::dispatch()
until_running
becomesfalse
, then optionally drain remaining items. stop()
swaps_subs
into a local map so destructors join safely without holding the global_mutex
.
Trait & Handle
traits::EventBusTrait<Event, Queue>
generates a static v-table for any bus satisfying theconcepts::EventBus
contract.EventBusRef<Event, Queue>
is a two-pointer type-erased handle that forwards all calls without virtual inheritance.
Notes
- No heap allocations after construction; queues are fixed-size.
- Thread-safe: publisher acquires
_mutex
only for queue pointers, not for per-event operations.