ILLIXR: Illinois Extended Reality Testbed
Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
ILLIXR::threadloop Class Referenceabstract

A reusable threadloop for plugins. More...

#include <threadloop.hpp>

Public Member Functions

 threadloop (const std::string &name_, phonebook *pb_)
 
void start () override
 Starts the thread. More...
 
void stop () override
 Joins the thread. More...
 
virtual void internal_stop ()
 Stops the thread. More...
 
- Public Member Functions inherited from ILLIXR::plugin
 plugin (std::string name_, phonebook *pb_)
 
std::string get_name () const noexcept
 
void spdlogger (const char *log_level)
 

Protected Types

enum class  skip_option { run , skip_and_spin , skip_and_yield , stop }
 

Protected Member Functions

virtual skip_option _p_should_skip ()
 Gets called in a tight loop, to gate the invocation of _p_one_iteration()
 
virtual void _p_thread_setup ()
 Gets called at setup time, from the new thread.
 
virtual void _p_one_iteration ()=0
 Override with the computation the thread does every loop. More...
 
bool should_terminate ()
 Whether the thread has been asked to terminate. More...
 

Protected Attributes

std::size_t iteration_no = 0
 
std::size_t skip_no = 0
 
- Protected Attributes inherited from ILLIXR::plugin
std::string name
 
const phonebookpb
 
const std::shared_ptr< record_loggerrecord_logger_
 
const std::shared_ptr< gen_guidgen_guid_
 
const std::size_t id
 

Detailed Description

A reusable threadloop for plugins.

The thread continuously runs _p_one_iteration() and is stopable by stop().

This factors out the common code I noticed in many different plugins.

Member Enumeration Documentation

◆ skip_option

enum ILLIXR::threadloop::skip_option
strongprotected
Enumerator
run 

Run iteration NOW. Only then does CPU timer begin counting.

skip_and_spin 

AKA "busy wait". Skip but try again very quickly.

skip_and_yield 

Yielding gives up a scheduling quantum, which is determined by the OS, but usually on the order of 1-10ms. This is nicer to the other threads in the system.

stop 

Calls stop.

Member Function Documentation

◆ _p_one_iteration()

virtual void ILLIXR::threadloop::_p_one_iteration ( )
protectedpure virtual

Override with the computation the thread does every loop.

This gets called in rapid succession.

◆ internal_stop()

virtual void ILLIXR::threadloop::internal_stop ( )
inlinevirtual

Stops the thread.

A thread should call this if it wants to stop itself (due to out of data for example).

◆ should_terminate()

bool ILLIXR::threadloop::should_terminate ( )
inlineprotected

Whether the thread has been asked to terminate.

Check this before doing long-running computation; it makes termination more responsive.

◆ start()

void ILLIXR::threadloop::start ( )
inlineoverridevirtual

Starts the thread.

This cannot go into the constructor because it starts a thread which calls _p_one_iteration() which is virtual in the child class.

Calling a virtual child method from the parent constructor will not work as expected 1. Instead, the ISO CPP FAQ recommends calling a start() method immediately after construction 2.

Reimplemented from ILLIXR::plugin.

◆ stop()

void ILLIXR::threadloop::stop ( )
inlineoverridevirtual

Joins the thread.

Must have already stopped the stoplight.

Reimplemented from ILLIXR::plugin.


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