neic-glass3  1.4.6
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
glass3::util::ThreadBaseClass Class Referenceabstract

glass3::util::ThreadBaseClass class More...

#include <threadbaseclass.h>

Inheritance diagram for glass3::util::ThreadBaseClass:
Inheritance graph
Collaboration diagram for glass3::util::ThreadBaseClass:
Collaboration graph

Public Member Functions

 ThreadBaseClass ()
 ThreadBaseClass constructor. More...
 
 ThreadBaseClass (std::string threadName, int sleepTimeMS=100, int numThreads=1, int checkInterval=300)
 An advanced constructor that sets up the ThreadBaseClass with a provided thread name, sleep between work time duration, number of threads, and statsus check interval. More...
 
 ~ThreadBaseClass ()
 ThreadBaseClass destructor. More...
 
virtual bool start ()
 work thread start function More...
 
virtual bool stop ()
 work thread stop function More...
 
int getNumThreads ()
 Retrieves the number of work threads. More...
 
void setNumThreads (int numThreads)
 Sets the number of work threads. More...
 
void setThreadHealth (bool health=true)
 Function to set thread health. More...
 
virtual bool healthCheck ()
 work threads check function More...
 
void setSleepTime (int sleepTimeMS)
 Sets the time to sleep between work() calls. More...
 
int getSleepTime ()
 Retrieves the time to sleep between work() calls. More...
 
glass3::util::ThreadState getWorkThreadsState ()
 Function to get the work threads state. More...
 
void setHealthCheckInterval (int interval)
 Function to set work threads health check interval. More...
 
int getHealthCheckInterval ()
 Function to retrieve the work threads health check interval. More...
 
bool getTerminate ()
 Function to retrieve whether the thread should terminate. More...
 
const std::string & getThreadName ()
 Function to retrieve the name of the work threads. More...
 
std::time_t getAllLastHealthy (size_t *pThreadId)
 Function to retrieve the oldest time any of the work threads last updated their health status as healthy. More...
 
virtual glass3::util::WorkState work ()=0
 ThreadBaseClass work function. More...
 
- Public Member Functions inherited from glass3::util::BaseClass
 BaseClass ()
 BaseClass constructor. More...
 
virtual ~BaseClass ()
 BaseClass destructor. More...
 
virtual bool setup (std::shared_ptr< const json::Object > config)
 BaseClass configuration function. More...
 
virtual void clear ()
 BaseClass clear function. More...
 
const std::shared_ptr< const json::Object > getConfig ()
 
bool getSetup ()
 Retrieves the class member boolean flag indicating whether the class has been setup, set to true if setup was successful. More...
 
const std::string & getDefaultAgencyId ()
 Function to retrieve the name of the default agency id. More...
 
void setDefaultAgencyId (const std::string &id)
 Function to set the name of the default agency id. More...
 
const std::string & getDefaultAuthor ()
 Function to retrieve the name of the default author. More...
 
void setDefaultAuthor (const std::string &author)
 Function to set the name of the default author. More...
 

Protected Member Functions

void setWorkThreadsState (glass3::util::ThreadState state)
 Function to set threads state. More...
 
void workLoop ()
 ThreadBaseClass work loop function. More...
 
void setLastHealthy (std::time_t now)
 Function to set the last time the work thread was healthy. More...
 

Protected Attributes

std::vector< std::thread > m_WorkThreads
 the std::vector that contains the work std::thread objects More...
 
std::map< std::thread::id, std::atomic< int > > m_ThreadHealthMap
 A std::map containing the epoch times as std::atomic<double>> that each work thread was last marked as healthy, identified by the thread id. More...
 
- Protected Attributes inherited from glass3::util::BaseClass
std::shared_ptr< const json::Object > m_Config
 A shared pointer to the json::Object that holds the configuration. More...
 
std::atomic< bool > m_bIsSetup
 the boolean flag indicating whether the class has been setup, set to true if setup was successful. More...
 
std::string m_DefaultAgencyID
 A std::string containing the default agency id to use in parsing if one is not provided. More...
 
std::string m_DefaultAuthor
 A std::string containing the default author to use in parsing if one is not provided. More...
 

Detailed Description

glass3::util::ThreadBaseClass class

This class supports creating, starting, stopping, and monitoring one or more work threads, that do the same work allowing for specific thread name and sleep between work time.

It is intended that the derived class implement the desired thread work by overriding the pure virtual function work()

This class inherits from util::BaseClass

Constructor & Destructor Documentation

glass3::util::ThreadBaseClass::ThreadBaseClass ( )

ThreadBaseClass constructor.

The constructor for the ThreadBaseClass class. Initializes members to default values.

glass3::util::ThreadBaseClass::ThreadBaseClass ( std::string  threadName,
int  sleepTimeMS = 100,
int  numThreads = 1,
int  checkInterval = 300 
)

An advanced constructor that sets up the ThreadBaseClass with a provided thread name, sleep between work time duration, number of threads, and statsus check interval.

The advanced constructor for the ThreadBaseClass class. Initializes members to provided values.

Parameters
threadName- A std::string containing the desired name of the work thread.
sleepTimeMS- An integer value containing the amount of time to sleep between work() calls in the work thread in milliseconds, default 100ms
numThreads- An integer containing the number of work threads. Default 1
checkInterval- An integer containing the amount of time in seconds between status checks. -1 to disable status checks. Default 300.
glass3::util::ThreadBaseClass::~ThreadBaseClass ( )

ThreadBaseClass destructor.

The destructor for the ThreadBaseClass class.

Member Function Documentation

std::time_t glass3::util::ThreadBaseClass::getAllLastHealthy ( size_t *  pThreadId)

Function to retrieve the oldest time any of the work threads last updated their health status as healthy.

This function retrieves the oldest time any of the the health statuses of the work threads was updated as healthy by the setThreadHealth function

Parameters
pThreadIda pointer to a size_t holding the oldest thread id
Returns
A std::time_t containing the last check time
int glass3::util::ThreadBaseClass::getHealthCheckInterval ( )

Function to retrieve the work threads health check interval.

This function retrieves the time interval after which an entry in m_ThreadHealthMap being older than now minus the time interval (not responded) indicates that the work thread has died in healthCheck()

Returns
An integer value containing the work thread health check interval in seconds
int glass3::util::ThreadBaseClass::getNumThreads ( )

Retrieves the number of work threads.

Retrieves the number of work threads managed by this class

Returns
Returns the number of work threads
int glass3::util::ThreadBaseClass::getSleepTime ( )

Retrieves the time to sleep between work() calls.

Retrieves the amount of time to sleep between work() function calls in the workLoop() function, which is run by the thread

Returns
Returns the amount of time to sleep between work() calls in integer milliseconds.
bool glass3::util::ThreadBaseClass::getTerminate ( )

Function to retrieve whether the thread should terminate.

This function retrieves internal terminate thread for use in derived classes

Returns
A boolean value indicating whether the thread should terminate
const std::string & glass3::util::ThreadBaseClass::getThreadName ( )

Function to retrieve the name of the work threads.

This function retrieves the name of the work threads, this name is used to identify the work threads

Returns
A std::string containing the thread name
glass3::util::ThreadState glass3::util::ThreadBaseClass::getWorkThreadsState ( )

Function to get the work threads state.

This function gets the state of the threads by getting the value of m_WorkThreadsState.

Returns
Returns a glass3::util::ThreadState enumeration value representing the threads state
bool glass3::util::ThreadBaseClass::healthCheck ( )
virtual

work threads check function

Checks to see if each thread that runs the workLoop() function is still operational, by checking the value of m_ThreadHealthMap[id] (==true) every m_iHealthCheckInterval seconds, setting m_ThreadHealthMap[id] to now after the check.

Returns
returns true if the pool is still running after m_iHealthCheckInterval seconds, false otherwise

Reimplemented in glass3::output::output, and glass3::process::Associator.

void glass3::util::ThreadBaseClass::setHealthCheckInterval ( int  interval)

Function to set work threads health check interval.

This function sets the time interval after which an entry in m_ThreadHealthMap being older than now minus the time interval (not responded) indicates that the work thread has died in healthCheck()

Parameters
interval= An integer value indicating the work thread health check interval in seconds
void glass3::util::ThreadBaseClass::setLastHealthy ( std::time_t  now)
protected

Function to set the last time the work thread was healthy.

This function sets the last time the work thread was healthy

Parameters
now- A std::time_t containing the last time the thread was healthy
void glass3::util::ThreadBaseClass::setNumThreads ( int  numThreads)

Sets the number of work threads.

This function sets m_iNumThreads. This function sets the number of work threads that should be running, and thus will be started in start().

Note
This function is only effective when the ThreadBaseClass is not started.
Parameters
numThreads- An integer containing the number of work threads
void glass3::util::ThreadBaseClass::setSleepTime ( int  sleepTimeMS)

Sets the time to sleep between work() calls.

Sets the amount of time to sleep between work() function calls in the workLoop() function, which is run by the thread

Parameters
sleepTimeMS- An integer value containing the sleep between work() calls in integer milliseconds.
void glass3::util::ThreadBaseClass::setThreadHealth ( bool  health = true)

Function to set thread health.

This function signifies the thread health by using setLastHealthy to set m_tLastHealthy for the current thread to now if health is true

Parameters
health= A boolean value indicating thread health, true indicates that setLastHealthy to set m_tLastHealthy to now, false indicates it should not
void glass3::util::ThreadBaseClass::setWorkThreadsState ( glass3::util::ThreadState  state)
protected

Function to set threads state.

This function signifies the threads state by setting m_WorkThreadsState to the provided value. This function is called by start(), stop(), and workLoop() to indicate various thread states during the thread startup and shutdown processes

Parameters
state= A glass3::util::ThreadState enumeration value indicating the new thread state
bool glass3::util::ThreadBaseClass::start ( )
virtual

work thread start function

Creates one or more thread objects to run the workLoop() function, and starts them, setting m_WorkThreadsState to be glass3::util::ThreadState::Starting.

Returns
returns true if successful, false if the thread creation failed or if a thread had already been started

Reimplemented in glass3::output::output.

bool glass3::util::ThreadBaseClass::stop ( )
virtual

work thread stop function

Stops, waits for, and deletes the threads that runs the workLoop() function, setting m_WorkThreadsState to glass3::util::ThreadState::Stopped

Returns
returns true if successful, false if the thread is not created and running
virtual glass3::util::WorkState glass3::util::ThreadBaseClass::work ( )
pure virtual

ThreadBaseClass work function.

This function is a pure virtual work function. It is intended that the derived class implement the desired thread work by overriding this function

Returns
This function returns glass3::util::WorkState, indicating whether the work was successful, encountered an error, or was idle (no work to perform)

Implemented in glasscore::CWeb, glass3::output::output, glasscore::CHypoList, glasscore::CSiteList, glasscore::CPickList, glass3::input::Input, glass3::process::Associator, and glass3::util::ThreadPool.

void glass3::util::ThreadBaseClass::workLoop ( )
protected

ThreadBaseClass work loop function.

This function is the thread work loop function. It runs in a loop while m_WorkThreadsState is glass3::util::ThreadState::Started, calls work() every m_iSleepTimeMS milliseconds, to do a unit of work, sets thread(s) health via setThreadHealth(). Setting m_WorkThreadsState to not equal glass3::util::ThreadState::Started via setWorkThreadsState, or a glass3::util::WorkState::Error return from work() will cause the loop to exit, and the function to return (ending the thread(s))

Member Data Documentation

std::map<std::thread::id, std::atomic<int> > glass3::util::ThreadBaseClass::m_ThreadHealthMap
protected

A std::map containing the epoch times as std::atomic<double>> that each work thread was last marked as healthy, identified by the thread id.

std::vector<std::thread> glass3::util::ThreadBaseClass::m_WorkThreads
protected

the std::vector that contains the work std::thread objects


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