libcyberradio  22.01.24
Thread Class Referenceabstract

Base class for a thread object, based on Boost Threads. More...

#include <Thread.h>

Inheritance diagram for Thread:
FlowControlClient StatusReceiver UdpStatusReceiver

Public Member Functions

 Thread (const std::string &name="", const std::string &cls="")
 Creates a Thread object. More...
 
 Thread (const Thread &src)
 Creates a Thread object by copying another Thread object. More...
 
virtual Threadoperator= (const Thread &src)
 Creates a Thread object by assignment from another Thread object. More...
 
virtual ~Thread ()
 Destroys a Thread object.
 
virtual void start ()
 Starts thread processing.
 
virtual void run ()=0
 Executes the main processing loop for the thread. More...
 
virtual void interrupt ()
 Interrupts (stops) the thread.
 
virtual void sleep (double secs)
 Pauses thread execution for a given time, checking for user interrupts during that time. More...
 
virtual bool isRunning () const
 Determines if the thread is running or not. More...
 
virtual void setName (const std::string &name)
 Sets the name of the thread. More...
 
virtual void setClass (const std::string &cls)
 Sets the class identifer string for the thread. More...
 
virtual boost::thread::id getId () const
 Gets the identifier of the underlying Boost thread. More...
 
virtual std::string getIdString () const
 Gets the identifier string for this thread. More...
 
virtual void onInterrupt ()
 Executes code that must run when the thread is interrupted. More...
 
virtual void onException (const std::exception &ex)
 Executes code that must run when an unhandled exception occurs within the thread. More...
 

Detailed Description

Base class for a thread object, based on Boost Threads.

Basic procedure for using this class:

  • Derive a threading class from Thread.
  • Override the run() method to provide the thread's processing loop.
  • Call the sleep() method on occasion within the run() method implementation to check for user interrupts.
  • Override onInterrupt() to perform processing that must be done when the thread is interrupted (stopped by the user).
  • Override onException() to perform processing that must be done when an unhandled exception occurs within the run() method.
  • Call the start() method from a higher-level thread or execution loop to begin thread processing.
  • Call the interrupt() method from a higher-level thread or execution loop to interrupt (stop) thread processing.
Note
Because this threading class is based on Boost Threads, users should use its sleep() method to do any waiting, because it checks for user interrupts within the Boost threading layer.

Definition at line 48 of file Thread.h.

Constructor & Destructor Documentation

◆ Thread() [1/2]

Thread ( const std::string &  name = "",
const std::string &  cls = "" 
)

Creates a Thread object.

Parameters
nameName of this thread.
clsClass identifier string for this thread.

Definition at line 19 of file Thread.cpp.

◆ Thread() [2/2]

Thread ( const Thread src)

Creates a Thread object by copying another Thread object.

Parameters
srcThe Thread object being copied.

Definition at line 27 of file Thread.cpp.

Member Function Documentation

◆ getId()

boost::thread::id getId ( ) const
virtual

Gets the identifier of the underlying Boost thread.

Returns
The thread identifier. If the thread is not running, this returns boost::thread().

Definition at line 91 of file Thread.cpp.

◆ getIdString()

std::string getIdString ( ) const
virtual

Gets the identifier string for this thread.

Returns
The thread identifier string.

Definition at line 96 of file Thread.cpp.

◆ isRunning()

bool isRunning ( ) const
virtual

Determines if the thread is running or not.

Returns
True if the thread is running, false otherwise.

Definition at line 72 of file Thread.cpp.

◆ onException()

void onException ( const std::exception &  ex)
virtual

Executes code that must run when an unhandled exception occurs within the thread.

The base-class method does nothing. Override this method in derived classes to perform custom exception processing.

Parameters
exThe exception that occurred.

Definition at line 110 of file Thread.cpp.

◆ onInterrupt()

void onInterrupt ( )
virtual

Executes code that must run when the thread is interrupted.

The base-class method does nothing. Override this method in derived classes to perform custom interrupt processing.

Definition at line 87 of file Thread.cpp.

◆ operator=()

Thread & operator= ( const Thread src)
virtual

Creates a Thread object by assignment from another Thread object.

Parameters
srcThe Thread object being copied.

Definition at line 34 of file Thread.cpp.

◆ run()

virtual void run ( )
pure virtual

Executes the main processing loop for the thread.

Override this method in derived classes to perform thread processing.

Implemented in FlowControlClient, StatusReceiver, and UdpStatusReceiver.

◆ setClass()

void setClass ( const std::string &  cls)
virtual

Sets the class identifer string for the thread.

Parameters
clsThe new class identifer for the thread.

Definition at line 82 of file Thread.cpp.

◆ setName()

void setName ( const std::string &  name)
virtual

Sets the name of the thread.

Parameters
nameThe new name of the thread.

Definition at line 77 of file Thread.cpp.

◆ sleep()

void sleep ( double  secs)
virtual

Pauses thread execution for a given time, checking for user interrupts during that time.

The sleep timer has microsecond resolution.

Parameters
secsNumber of seconds to "sleep".

Definition at line 65 of file Thread.cpp.


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