MADARA  3.1.8
madara::threads::Threader Class Reference

Starts threads with first class support of MADARA contexts. More...

#include <Threader.h>

Collaboration diagram for madara::threads::Threader:

Public Member Functions

 Threader ()
 Default constructor. More...
 
 Threader (knowledge::KnowledgeBase &data_plane)
 Constructor. More...
 
 ~Threader ()
 Destructor. More...
 
void change_hertz (const std::string name, double hertz)
 Modify hertz rate of a thread. More...
 
knowledge::KnowledgeBase get_control_plane (void)
 Gets the control plane used by threads. More...
 
knowledge::KnowledgeBase get_data_plane (void)
 Gets the data plane used by threads. More...
 
void pause (const std::string name)
 Requests a specific thread to pause. More...
 
void pause (void)
 Requests all threads to pause. More...
 
void resume (const std::string name)
 Requests a specific thread to resume (unpause) More...
 
void resume (void)
 Requests all threads to resume (unpause) More...
 
void run (const std::string name, BaseThread *thread, bool paused=false)
 Starts a new thread and executes the provided user thread once. More...
 
void run (double hertz, const std::string name, BaseThread *thread, bool paused=false)
 Starts a new thread and executes the provided user thread at a hertz. More...
 
void set_data_plane (knowledge::KnowledgeBase &data_plane)
 Sets the data plane for new threads. More...
 
void terminate (const std::string name)
 Requests a specific thread to terminate. More...
 
void terminate (void)
 Requests all threads to terminate. More...
 
bool wait (const std::string name, const knowledge::WaitSettings &ws=knowledge::WaitSettings())
 Wait for a specific thread to complete. More...
 
bool wait (const knowledge::WaitSettings &ws=knowledge::WaitSettings())
 Wait for all threads to complete. More...
 

Private Attributes

knowledge::KnowledgeBasecontrol_
 The control plane used by threads for termination and pause information. More...
 
knowledge::KnowledgeBasedata_
 The data plane used by threads. More...
 
NamedWorkerThreads threads_
 the threads that are still active More...
 

Detailed Description

Starts threads with first class support of MADARA contexts.

Definition at line 35 of file Threader.h.

Constructor & Destructor Documentation

madara::threads::Threader::Threader ( )

Default constructor.

Definition at line 10 of file Threader.cpp.

madara::threads::Threader::Threader ( knowledge::KnowledgeBase data_plane)

Constructor.

Parameters
data_planeThe data plane for threads to use

Definition at line 16 of file Threader.cpp.

madara::threads::Threader::~Threader ( )

Destructor.

Definition at line 22 of file Threader.cpp.

Member Function Documentation

void madara::threads::Threader::change_hertz ( const std::string  name,
double  hertz 
)
inline

Modify hertz rate of a thread.

This is only useful for periodic threads that are operating at infinite or set hertz rates. If the thread had been started as a run once thread, it cannot be changed to a hertz rate as the thread will be dead. In the latter case, restart the thread at the new hertz rate.

Parameters
nameunique thread name for the thread
hertznew hertz rate for the periodic thread

Definition at line 17 of file Threader.inl.

madara::knowledge::KnowledgeBase madara::threads::Threader::get_control_plane ( void  )
inline

Gets the control plane used by threads.

Returns
the knowledge base used by threader for control of threads

Definition at line 33 of file Threader.inl.

madara::knowledge::KnowledgeBase madara::threads::Threader::get_data_plane ( void  )
inline

Gets the data plane used by threads.

Returns
the knowledge base used by threads for data

Definition at line 23 of file Threader.inl.

void madara::threads::Threader::pause ( const std::string  name)

Requests a specific thread to pause.

Parameters
nameunique thread name for the thread.

Definition at line 31 of file Threader.cpp.

void madara::threads::Threader::pause ( void  )

Requests all threads to pause.

Definition at line 42 of file Threader.cpp.

void madara::threads::Threader::resume ( const std::string  name)

Requests a specific thread to resume (unpause)

Parameters
nameunique thread name for the thread.

Definition at line 52 of file Threader.cpp.

void madara::threads::Threader::resume ( void  )

Requests all threads to resume (unpause)

Definition at line 63 of file Threader.cpp.

void madara::threads::Threader::run ( const std::string  name,
BaseThread thread,
bool  paused = false 
)

Starts a new thread and executes the provided user thread once.

Execution ordering is init -> execute -> cleanup.


 
The thread will be managed by the threader. Please do not pass the address of a thread on the stack and do not delete this memory yourself or your program will crash.

Parameters
nameunique thread name for the thread. If possible, try to use one word or words separated by underscores (_)
threaduser-created thread implementation
pausedcreate thread in a paused state.

Definition at line 73 of file Threader.cpp.

void madara::threads::Threader::run ( double  hertz,
const std::string  name,
BaseThread thread,
bool  paused = false 
)

Starts a new thread and executes the provided user thread at a hertz.

Execution ordering is init -> *execute -> cleanup. init and cleanup are only called once.


 
The thread will be managed by the threader. Please do not pass the address of a thread on the stack and do not delete this memory yourself or your program will crash.

Parameters
hertzthe intended hertz (frequency) that the thread's execute should be ran. Hertz is in operations per second. 0.0 means infinite hertz rate. -1 means a one shot thread.
nameunique thread name for the thread. If possible, try to use one word or words separated by underscores (_)
threaduser-created thread implementation
pausedcreate thread in a paused state.

Definition at line 126 of file Threader.cpp.

void madara::threads::Threader::set_data_plane ( knowledge::KnowledgeBase data_plane)

Sets the data plane for new threads.

Parameters
data_planeThe data plane for threads to use

Definition at line 143 of file Threader.cpp.

void madara::threads::Threader::terminate ( const std::string  name)

Requests a specific thread to terminate.

Parameters
nameunique thread name for the thread.

Definition at line 150 of file Threader.cpp.

void madara::threads::Threader::terminate ( void  )

Requests all threads to terminate.

Definition at line 161 of file Threader.cpp.

bool madara::threads::Threader::wait ( const std::string  name,
const knowledge::WaitSettings ws = knowledge::WaitSettings () 
)

Wait for a specific thread to complete.

Parameters
nameunique thread name for the thread
wswait settings for specifying period and timeout
Returns
true if thread was terminated, false if timeout

Definition at line 171 of file Threader.cpp.

bool madara::threads::Threader::wait ( const knowledge::WaitSettings ws = knowledge::WaitSettings ())

Wait for all threads to complete.

Parameters
wswait settings for specifying period and timeout
Returns
true if thread was terminated, false if timeout

Definition at line 198 of file Threader.cpp.

Member Data Documentation

knowledge::KnowledgeBase* madara::threads::Threader::control_
private

The control plane used by threads for termination and pause information.

This has to be on the heap, because each thread gets its own stack!

Definition at line 227 of file Threader.h.

knowledge::KnowledgeBase* madara::threads::Threader::data_
private

The data plane used by threads.

Definition at line 220 of file Threader.h.

NamedWorkerThreads madara::threads::Threader::threads_
private

the threads that are still active

Definition at line 232 of file Threader.h.


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