MT Core (C++)
Core library for replacing C++ standard in project usage
|
A broadcaster is able to send messages to multiple channels, with each channel having its own lock Sending messages requires locking the entire broadcaster. More...
#include <broadcaster.hpp>
Public Member Functions | |
Result< void, AllocationError > | init (Allocator &alloc, size_t initCapacity=64) |
Initializes a broadcaster. | |
Result< void, ChannelSendBlockErrors > | send_block (Allocator &alloc, const T &msg) |
Blocks while sending data. | |
Result< BroadcastSubscription< T >, BroadcastSubscribeError > | subscribe (Allocator &alloc) |
Subscribes to a broadcaster Will create a new inbox for the subscription. | |
void | close () |
Closes the broadcaster which prevents messages from being sent (which aren't already being sent) and it also prevents new subscriptions from being created It will also close all existing subscriptions, which can result in partial writes (e.g. | |
void | deinit (Allocator &alloc) |
Will close the broadcaster and will also clean up the broadcaster's references to each subscription (a single Arc.release()) Will also close any subscriptions that didn't get cleaned up. | |
Public Attributes | |
GenList< BroadcastSubscription< T > > | channels |
std::mutex | mux |
bool | closed = false |
A broadcaster is able to send messages to multiple channels, with each channel having its own lock Sending messages requires locking the entire broadcaster.
However, reading messages allows for many separate locks. This makes it ideal for scenarios with one publisher and many subscribers.
Note that there is less insight into the state of each channel as they are locked separately. Because of this, only blocking sends and blocking receives are allowed, as acquiring the lock and waiting is the only way to really prevent partial sends.
So, while we should have better read throughput than the Publisher/Subscription flow, we have less flexibility in the API and only have blocking operations. This also means we cannot use Broadcaster with select()
Locks:
Atomic Reference Counting:
Condition Variables:
Non-Blocking Availability:
Known Race Conditions (aka. designed in):
T | Message Type |
Definition at line 103 of file broadcaster.hpp.
|
inline |
Closes the broadcaster which prevents messages from being sent (which aren't already being sent) and it also prevents new subscriptions from being created It will also close all existing subscriptions, which can result in partial writes (e.g.
a message is blocked after sending to half of the subscriptions)
Definition at line 201 of file broadcaster.hpp.
|
inline |
Will close the broadcaster and will also clean up the broadcaster's references to each subscription (a single Arc.release()) Will also close any subscriptions that didn't get cleaned up.
alloc | Allocator to free memory with |
Definition at line 219 of file broadcaster.hpp.
|
inline |
Initializes a broadcaster.
alloc | Allocator for allocating memory |
initCapacity | Initial capacity |
Definition at line 114 of file broadcaster.hpp.
|
inline |
Blocks while sending data.
Will grow inboxes as needed, and will free closed subscriptions as needed
alloc | Allocator to use for growing and closing |
msg | Message to send |
Definition at line 124 of file broadcaster.hpp.
|
inline |
Subscribes to a broadcaster Will create a new inbox for the subscription.
alloc | Allocator to use for creating the subscription |
Definition at line 168 of file broadcaster.hpp.
GenList<BroadcastSubscription<T> > mtcore::thread::broadcaster< T >::channels |
Definition at line 104 of file broadcaster.hpp.
bool mtcore::thread::broadcaster< T >::closed = false |
Definition at line 106 of file broadcaster.hpp.
std::mutex mtcore::thread::broadcaster< T >::mux |
Definition at line 105 of file broadcaster.hpp.