Goby3  3.1.4
2024.02.22
goby::middleware::Thread< Config, TransporterType > Class Template Reference

Represents a thread of execution within the Goby middleware, interleaving periodic events (loop()) with asynchronous receipt of data. Most user code should inherit from SimpleThread, not from Thread directly. More...

#include <goby/middleware/application/thread.h>

Public Types

using Transporter = TransporterType
 

Public Member Functions

 Thread (const Config &cfg, TransporterType *transporter, int index)
 Construct a thread with a given configuration, underlying transporter, and index (for multiple instantiations), but without any loop() frequency. More...
 
 Thread (const Config &cfg, TransporterType *transporter, double loop_freq_hertz=0, int index=-1)
 Construct a thread with all possible metadata (using double to specify frequency in Hertz) More...
 
 Thread (const Config &cfg, TransporterType *transporter, boost::units::quantity< boost::units::si::frequency > loop_freq, int index=-1)
 Construct a thread with all possible metadata (using boost::units to specify frequency) More...
 
virtual ~Thread ()
 
void run (std::atomic< bool > &alive)
 Run the thread until the boolean reference passed is set false. This call blocks, and should be run in a std::thread by the caller. More...
 
int index () const
 
std::type_index type_index ()
 
void set_type_index (std::type_index type_i)
 
std::string name ()
 
void set_name (const std::string &name)
 
int uid ()
 
void set_uid (int uid)
 

Static Public Attributes

static constexpr goby::middleware::Group shutdown_group_ {"goby::middleware::Thread::shutdown"}
 
static constexpr goby::middleware::Group joinable_group_ {"goby::middleware::Thread::joinable"}
 

Protected Member Functions

 Thread (const Config &cfg, boost::units::quantity< boost::units::si::frequency > loop_freq, int index=-1)
 
void set_transporter (TransporterType *transporter)
 
virtual void loop ()
 
double loop_frequency_hertz () const
 
decltype(loop_frequency_) loop_frequency () const
 
double loop_max_frequency () const
 
void run_once ()
 
TransporterType & transporter () const
 
const Config & cfg () const
 
virtual void initialize ()
 
virtual void finalize ()
 
void thread_health (goby::middleware::protobuf::ThreadHealth &health)
 
virtual void health (goby::middleware::protobuf::ThreadHealth &health)
 Called when HealthRequest is made by goby_coroner. More...
 
void thread_quit ()
 
bool alive ()
 

Detailed Description

template<typename Config, typename TransporterType>
class goby::middleware::Thread< Config, TransporterType >

Represents a thread of execution within the Goby middleware, interleaving periodic events (loop()) with asynchronous receipt of data. Most user code should inherit from SimpleThread, not from Thread directly.

A Thread can represent the main thread of an application or a thread that was launched after startup.

Template Parameters
ConfigType of the configuration for thie code running in this Thread
TransporterTypeType of the underlying transporter used for publish/subscribe from this thread

Definition at line 60 of file thread.h.

Member Typedef Documentation

◆ Transporter

template<typename Config , typename TransporterType >
using goby::middleware::Thread< Config, TransporterType >::Transporter = TransporterType

Definition at line 88 of file thread.h.

Constructor & Destructor Documentation

◆ Thread() [1/4]

template<typename Config , typename TransporterType >
goby::middleware::Thread< Config, TransporterType >::Thread ( const Config &  cfg,
TransporterType *  transporter,
int  index 
)
inline

Construct a thread with a given configuration, underlying transporter, and index (for multiple instantiations), but without any loop() frequency.

Parameters
cfgData to configure the code running in this thread
transporterUnderlying transporter
indexNumeric index to identify this instantiation of the Thread (only necessary if multiple Threads of the same type are created) Note: loop() will never be called when using this constructor

Definition at line 96 of file thread.h.

◆ Thread() [2/4]

template<typename Config , typename TransporterType >
goby::middleware::Thread< Config, TransporterType >::Thread ( const Config &  cfg,
TransporterType *  transporter,
double  loop_freq_hertz = 0,
int  index = -1 
)
inline

Construct a thread with all possible metadata (using double to specify frequency in Hertz)

Parameters
cfgData to configure the code running in this thread
transporterUnderlying transporter
loop_freq_hertzThe frequency at which to attempt to call loop(), assuming the thread isn't blocked handling transporter callbacks (e.g. subscribe callbacks)
indexNumeric index to identify this instantiation of the Thread (only necessary if multiple Threads of the same type are created)

Definition at line 107 of file thread.h.

◆ Thread() [3/4]

template<typename Config , typename TransporterType >
goby::middleware::Thread< Config, TransporterType >::Thread ( const Config &  cfg,
TransporterType *  transporter,
boost::units::quantity< boost::units::si::frequency >  loop_freq,
int  index = -1 
)
inline

Construct a thread with all possible metadata (using boost::units to specify frequency)

Parameters
cfgData to configure the code running in this thread
transporterUnderlying transporter
loop_freqThe frequency at which to attempt to call loop(), assuming the thread isn't blocked handling transporter callbacks (i.e. subscribe callbacks)
indexNumeric index to identify this instantiation of the Thread (only necessary if multiple Threads of the same type are created)

Definition at line 119 of file thread.h.

◆ ~Thread()

template<typename Config , typename TransporterType >
virtual goby::middleware::Thread< Config, TransporterType >::~Thread ( )
inlinevirtual

Definition at line 126 of file thread.h.

◆ Thread() [4/4]

template<typename Config , typename TransporterType >
goby::middleware::Thread< Config, TransporterType >::Thread ( const Config &  cfg,
boost::units::quantity< boost::units::si::frequency >  loop_freq,
int  index = -1 
)
inlineprotected

Definition at line 160 of file thread.h.

Member Function Documentation

◆ alive()

template<typename Config , typename TransporterType >
bool goby::middleware::Thread< Config, TransporterType >::alive ( )
inlineprotected

Definition at line 240 of file thread.h.

◆ cfg()

template<typename Config , typename TransporterType >
const Config& goby::middleware::Thread< Config, TransporterType >::cfg ( ) const
inlineprotected

Definition at line 201 of file thread.h.

◆ finalize()

template<typename Config , typename TransporterType >
virtual void goby::middleware::Thread< Config, TransporterType >::finalize ( )
inlineprotectedvirtual

Reimplemented in goby::middleware::io::detail::IOThread< line_in_group, line_out_group, PubSubLayer::INTERPROCESS, PubSubLayer::INTERTHREAD, goby::middleware::protobuf::PTYConfig, boost::asio::posix::stream_descriptor, goby::middleware::SimpleThread, false >, goby::middleware::io::detail::IOThread< line_in_group, line_out_group, PubSubLayer::INTERPROCESS, PubSubLayer::INTERTHREAD, goby::middleware::protobuf::CanConfig, boost::asio::posix::stream_descriptor, goby::middleware::SimpleThread, false >, goby::middleware::io::detail::IOThread< line_in_group, line_out_group, publish_layer, subscribe_layer, goby::middleware::protobuf::TCPClientConfig, boost::asio::ip::tcp::socket, goby::middleware::SimpleThread, use_indexed_groups >, goby::middleware::io::detail::IOThread< line_in_group, line_out_group, publish_layer, subscribe_layer, goby::middleware::protobuf::SerialConfig, boost::asio::serial_port, goby::middleware::SimpleThread, use_indexed_groups >, goby::middleware::io::detail::IOThread< line_in_group, line_out_group, PubSubLayer::INTERPROCESS, PubSubLayer::INTERTHREAD, goby::middleware::protobuf::UDPOneToManyConfig, boost::asio::ip::udp::socket, goby::middleware::SimpleThread, false >, goby::middleware::io::detail::IOThread< line_in_group, line_out_group, publish_layer, subscribe_layer, goby::middleware::protobuf::PTYConfig, boost::asio::posix::stream_descriptor, goby::middleware::SimpleThread, use_indexed_groups >, goby::middleware::io::detail::IOThread< line_in_group, line_out_group, publish_layer, subscribe_layer, goby::middleware::protobuf::TCPServerConfig, boost::asio::ip::tcp::acceptor, goby::middleware::SimpleThread, use_indexed_groups >, and goby::middleware::io::detail::IOThread< line_in_group, line_out_group, publish_layer, subscribe_layer, goby::middleware::protobuf::UDPPointToPointConfig, boost::asio::ip::udp::socket, goby::middleware::SimpleThread, use_indexed_groups >.

Definition at line 207 of file thread.h.

◆ health()

template<typename Config , typename TransporterType >
virtual void goby::middleware::Thread< Config, TransporterType >::health ( goby::middleware::protobuf::ThreadHealth health)
inlineprotectedvirtual

Called when HealthRequest is made by goby_coroner.

Override to implement thread specific health response

Reimplemented in goby::middleware::MultiThreadApplication< Config, InterProcessPortal >, and goby::middleware::SingleThreadApplication< Config, InterProcessPortal >.

Definition at line 225 of file thread.h.

◆ index()

template<typename Config , typename TransporterType >
int goby::middleware::Thread< Config, TransporterType >::index ( ) const
inline
Returns
the Thread index (for multiple instantiations)

Definition at line 145 of file thread.h.

◆ initialize()

template<typename Config , typename TransporterType >
virtual void goby::middleware::Thread< Config, TransporterType >::initialize ( )
inlineprotectedvirtual

Reimplemented in goby::middleware::io::detail::IOThread< line_in_group, line_out_group, PubSubLayer::INTERPROCESS, PubSubLayer::INTERTHREAD, goby::middleware::protobuf::PTYConfig, boost::asio::posix::stream_descriptor, goby::middleware::SimpleThread, false >, goby::middleware::io::detail::IOThread< line_in_group, line_out_group, PubSubLayer::INTERPROCESS, PubSubLayer::INTERTHREAD, goby::middleware::protobuf::CanConfig, boost::asio::posix::stream_descriptor, goby::middleware::SimpleThread, false >, goby::middleware::io::detail::IOThread< line_in_group, line_out_group, publish_layer, subscribe_layer, goby::middleware::protobuf::TCPClientConfig, boost::asio::ip::tcp::socket, goby::middleware::SimpleThread, use_indexed_groups >, goby::middleware::io::detail::IOThread< line_in_group, line_out_group, publish_layer, subscribe_layer, goby::middleware::protobuf::SerialConfig, boost::asio::serial_port, goby::middleware::SimpleThread, use_indexed_groups >, goby::middleware::io::detail::IOThread< line_in_group, line_out_group, PubSubLayer::INTERPROCESS, PubSubLayer::INTERTHREAD, goby::middleware::protobuf::UDPOneToManyConfig, boost::asio::ip::udp::socket, goby::middleware::SimpleThread, false >, goby::middleware::io::detail::IOThread< line_in_group, line_out_group, publish_layer, subscribe_layer, goby::middleware::protobuf::PTYConfig, boost::asio::posix::stream_descriptor, goby::middleware::SimpleThread, use_indexed_groups >, goby::middleware::io::detail::IOThread< line_in_group, line_out_group, publish_layer, subscribe_layer, goby::middleware::protobuf::TCPServerConfig, boost::asio::ip::tcp::acceptor, goby::middleware::SimpleThread, use_indexed_groups >, and goby::middleware::io::detail::IOThread< line_in_group, line_out_group, publish_layer, subscribe_layer, goby::middleware::protobuf::UDPPointToPointConfig, boost::asio::ip::udp::socket, goby::middleware::SimpleThread, use_indexed_groups >.

Definition at line 204 of file thread.h.

◆ loop()

template<typename Config , typename TransporterType >
virtual void goby::middleware::Thread< Config, TransporterType >::loop ( )
inlineprotectedvirtual

◆ loop_frequency()

template<typename Config , typename TransporterType >
decltype(loop_frequency_) goby::middleware::Thread< Config, TransporterType >::loop_frequency ( ) const
inlineprotected

Definition at line 195 of file thread.h.

◆ loop_frequency_hertz()

template<typename Config , typename TransporterType >
double goby::middleware::Thread< Config, TransporterType >::loop_frequency_hertz ( ) const
inlineprotected

Definition at line 194 of file thread.h.

◆ loop_max_frequency()

template<typename Config , typename TransporterType >
double goby::middleware::Thread< Config, TransporterType >::loop_max_frequency ( ) const
inlineprotected

Definition at line 196 of file thread.h.

◆ name()

template<typename Config , typename TransporterType >
std::string goby::middleware::Thread< Config, TransporterType >::name ( )
inline

Definition at line 150 of file thread.h.

◆ run()

template<typename Config , typename TransporterType >
void goby::middleware::Thread< Config, TransporterType >::run ( std::atomic< bool > &  alive)
inline

Run the thread until the boolean reference passed is set false. This call blocks, and should be run in a std::thread by the caller.

Parameters
aliveReference to an atomic boolean. While alive is true, the thread will run; when alive is set false, the thread will complete (and become joinable), assuming nothing is blocking loop() or any transporter callback.

Definition at line 131 of file thread.h.

◆ run_once()

template<typename Config , typename TransporterType >
void goby::middleware::Thread< Config, TransporterType >::run_once
protected

Definition at line 275 of file thread.h.

◆ set_name()

template<typename Config , typename TransporterType >
void goby::middleware::Thread< Config, TransporterType >::set_name ( const std::string &  name)
inline

Definition at line 151 of file thread.h.

◆ set_transporter()

template<typename Config , typename TransporterType >
void goby::middleware::Thread< Config, TransporterType >::set_transporter ( TransporterType *  transporter)
inlineprotected

Definition at line 186 of file thread.h.

◆ set_type_index()

template<typename Config , typename TransporterType >
void goby::middleware::Thread< Config, TransporterType >::set_type_index ( std::type_index  type_i)
inline

Definition at line 148 of file thread.h.

◆ set_uid()

template<typename Config , typename TransporterType >
void goby::middleware::Thread< Config, TransporterType >::set_uid ( int  uid)
inline

Definition at line 154 of file thread.h.

◆ thread_health()

template<typename Config , typename TransporterType >
void goby::middleware::Thread< Config, TransporterType >::thread_health ( goby::middleware::protobuf::ThreadHealth health)
inlineprotected

Definition at line 209 of file thread.h.

◆ thread_quit()

template<typename Config , typename TransporterType >
void goby::middleware::Thread< Config, TransporterType >::thread_quit ( )
inlineprotected

Definition at line 230 of file thread.h.

◆ transporter()

template<typename Config , typename TransporterType >
TransporterType& goby::middleware::Thread< Config, TransporterType >::transporter ( ) const
inlineprotected

Definition at line 199 of file thread.h.

◆ type_index()

template<typename Config , typename TransporterType >
std::type_index goby::middleware::Thread< Config, TransporterType >::type_index ( )
inline

Definition at line 147 of file thread.h.

◆ uid()

template<typename Config , typename TransporterType >
int goby::middleware::Thread< Config, TransporterType >::uid ( )
inline

Definition at line 153 of file thread.h.

Member Data Documentation

◆ joinable_group_

template<typename Config , typename TransporterType >
constexpr goby::middleware::Group goby::middleware::Thread< Config, TransporterType >::joinable_group_ {"goby::middleware::Thread::joinable"}
staticconstexpr

Definition at line 157 of file thread.h.

◆ shutdown_group_

template<typename Config , typename TransporterType >
constexpr goby::middleware::Group goby::middleware::Thread< Config, TransporterType >::shutdown_group_ {"goby::middleware::Thread::shutdown"}
staticconstexpr

Definition at line 156 of file thread.h.


The documentation for this class was generated from the following file: