MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
Inter-Thread Communication

Manages inter-thread communication (and synchronization) More...

Classes

struct  mtcore::thread::BroadcastSubscription< T >
 A subscription to a broadcaster. More...
 
struct  mtcore::thread::broadcaster< T >
 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...
 
struct  mtcore::thread::Channel< T, Size >
 Channel is a message-passing primitive between two threads A channel has blocking and non-blocking (or auto timed out for a size of 0) methods Timeout methods are also present A channel will have a buffer size for how many messages can be queued up If there is a buffer size of 0, then a reader and writer must both be accessing the channel simultaneously for messages to be transmitted. More...
 
struct  mtcore::thread::Future< T, E >
 Represents a value that will be made available in the future Can wait on the value for when it is ready. More...
 
struct  mtcore::thread::FutureState< T, void >
 Represents the state-setter part of a future. More...
 
struct  mtcore::thread::FutureState< T, E >
 Represents the state-setter part of a future. More...
 
struct  mtcore::thread::Inbox< T >
 Inter-thread communication primitive to send messages between threads Has a growable Queue. More...
 
struct  mtcore::thread::Subscription< T >
 Represents a pub/sub subscription which can receive messages of type T Under the hood, each publisher maintains a message broker which stores all messages Subscribers will hold an Arc to the broker and will retrieve messages from the broker Due to the broker being shared, it does require more locks for both reads and writes This means it has a pretty low throughput when there is lots of contention The primary advantage is it has a much more convenient API for more non-blocking operations or timed blocks The reason this happens is that publishers are given greater insight into each subscription, and can better avoid partial writes. More...
 
struct  mtcore::thread::Publisher< T >
 Represents a pub/sub publisher which can publish messages of type T Supports blocking and non-blocking publishing. More...
 
struct  mtcore::thread::Select< Options >
 Underlying type for a select statement Use the "select" method directly for instantiation. More...
 

Functions

template<SelectOption... Options>
Result< void, SelectErrorsmtcore::thread::select (Options... opts)
 Creates a "select statement" similar to Go's select statement, except this statement runs on thread-based primitives instead of coroutine-based primitives.
 
template<Sender S, typename Callback>
SendOption< S, Callback > mtcore::thread::send_to (S &to, typename S::Message msg, const Callback &callback)
 Creates a select case which will try to send a message to a sender Option will call a callback on successful send.
 
template<Sender S>
SendOptionNoCallback< S > mtcore::thread::send_to (S &to, typename S::Message msg)
 Creates a select case which will try to send a message to a sender.
 
template<typename Clock, typename Duration, typename Callback>
BeforeOption< Clock, Duration, Callback > mtcore::thread::before (const std::chrono::time_point< Clock, Duration > &tp, const Callback &callback)
 The before option will timeout a select if an option is not hit before the target time point.
 
template<typename Clock, typename Duration>
BeforeOptionNoCallback< Clock, Duration > mtcore::thread::before (const std::chrono::time_point< Clock, Duration > &tp)
 The before option will timeout a select if an option is not hit before the target time point.
 
template<typename Rep, typename Per, typename Callback>
auto mtcore::thread::timeout (const std::chrono::duration< Rep, Per > &tp, const Callback &callback)
 Does a timeout relative to current time (e.g.
 
template<typename Rep, typename Per>
auto mtcore::thread::timeout (const std::chrono::duration< Rep, Per > &tp)
 Does a timeout relative to current time (e.g.
 
template<Receiver R, typename Callback>
ReceiveOption< R, Callback > mtcore::thread::receive_from (R &from, const Callback &callback)
 Creates a receive option for select which will execute when a message is ready.
 

Detailed Description

Manages inter-thread communication (and synchronization)

Function Documentation

◆ before() [1/2]

template<typename Clock, typename Duration>
BeforeOptionNoCallback< Clock, Duration > mtcore::thread::before ( const std::chrono::time_point< Clock, Duration > & tp)

The before option will timeout a select if an option is not hit before the target time point.

Template Parameters
ClockClock for getting the time
DurationDuration of the ending time point
Parameters
tpTime point at which timeout will be called
Returns
Before option for select

Definition at line 248 of file select.hpp.

248 {
250 }
Option to add a timeout on select to ensure a path runs before a timeout If the timeout is hit,...
Definition select.hpp:205

◆ before() [2/2]

template<typename Clock, typename Duration, typename Callback>
BeforeOption< Clock, Duration, Callback > mtcore::thread::before ( const std::chrono::time_point< Clock, Duration > & tp,
const Callback & callback )

The before option will timeout a select if an option is not hit before the target time point.

Template Parameters
ClockClock for getting the time
DurationDuration of the ending time point
CallbackCallback type to call
Parameters
tpTime point at which timeout will be called
callbackCallback to call on timeout
Returns
Before option for select

Definition at line 233 of file select.hpp.

234 {
235 return BeforeOption<Clock, Duration, Callback>{tp, callback};
236 }
Option to add a timeout on select to ensure a path runs before a timeout If the timeout is hit,...
Definition select.hpp:180
Here is the caller graph for this function:

◆ receive_from()

template<Receiver R, typename Callback>
ReceiveOption< R, Callback > mtcore::thread::receive_from ( R & from,
const Callback & callback )

Creates a receive option for select which will execute when a message is ready.

Template Parameters
RReceiver type to receive from
CallbackCallback type to call on receive (compatible with std::function<void(*)(const typename R::Message&)>)
Parameters
fromReceiver to receive from
callbackCallback on receive
Returns
Receive option for select

Definition at line 316 of file select.hpp.

316 {
317 return ReceiveOption<R, Callback>{from, callback};
318 }
Select option to receive a message and calls a callback on receive.
Definition select.hpp:286

◆ select()

template<SelectOption... Options>
Result< void, SelectErrors > mtcore::thread::select ( Options... opts)
nodiscard

Creates a "select statement" similar to Go's select statement, except this statement runs on thread-based primitives instead of coroutine-based primitives.

The Result will be an error if either:

  • A select timeout occurs (i.e. a before option expires)
  • A deadlock is detected (e.g. a select of only send has all sending channels close)
    Template Parameters
    OptionsTypes of options to wait on
    Parameters
    optsList of options to wait on
    Returns
    Result of running the select statement

Definition at line 417 of file select.hpp.

417 {
418 return Select<Options...>{opts...}();
419 }
Underlying type for a select statement Use the "select" method directly for instantiation.
Definition select.hpp:56

◆ send_to() [1/2]

template<Sender S>
SendOptionNoCallback< S > mtcore::thread::send_to ( S & to,
typename S::Message msg )

Creates a select case which will try to send a message to a sender.

Template Parameters
SSender type to send a message to
Parameters
toSender to send a message to
msgMessage to send
Returns
Send option to pass to select()

Definition at line 167 of file select.hpp.

167 {
168 return SendOptionNoCallback<S>{to, msg};
169 }
Select option to send a message without calling a callback on send.
Definition select.hpp:80

◆ send_to() [2/2]

template<Sender S, typename Callback>
SendOption< S, Callback > mtcore::thread::send_to ( S & to,
typename S::Message msg,
const Callback & callback )

Creates a select case which will try to send a message to a sender Option will call a callback on successful send.

Template Parameters
SSender type to send a message to
CallbackCallback type on success (compatible with std::function<void (void)>)
Parameters
toSender to send a message to
msgMessage to send
callbackCallback to call on success
Returns
Send option to pass to select()

Definition at line 154 of file select.hpp.

154 {
155 return SendOption<S, Callback>{to, msg, callback};
156 }
Select option to send a message.
Definition select.hpp:113

◆ timeout() [1/2]

template<typename Rep, typename Per>
auto mtcore::thread::timeout ( const std::chrono::duration< Rep, Per > & tp)

Does a timeout relative to current time (e.g.

now() + 5ms)

Template Parameters
RepDuration representation
PerDuration period
Parameters
tpTime point at which timeout will be called
Returns
Before option for select

Definition at line 276 of file select.hpp.

276 {
277 return before(std::chrono::steady_clock::now() + tp);
278 }
BeforeOption< Clock, Duration, Callback > before(const std::chrono::time_point< Clock, Duration > &tp, const Callback &callback)
The before option will timeout a select if an option is not hit before the target time point.
Definition select.hpp:233
Here is the call graph for this function:

◆ timeout() [2/2]

template<typename Rep, typename Per, typename Callback>
auto mtcore::thread::timeout ( const std::chrono::duration< Rep, Per > & tp,
const Callback & callback )

Does a timeout relative to current time (e.g.

now() + 5ms)

Template Parameters
RepDuration representation
PerDuration period
CallbackCallback type to call
Parameters
tpTime point at which timeout will be called
callbackCallback to call on timeout
Returns
Before option for select

Definition at line 263 of file select.hpp.

263 {
264 return before(std::chrono::steady_clock::now() + tp, callback);
265 }
Here is the call graph for this function: