This class implements a worker thread that receives jobs from a private queue. Jobs can have a different Priority, and thus may not be executed in the order of insertion. This concept comprises a very simple scheduling approach and should not be overused. In the end, any multithreading software has to be designed in a way that a job queue never grows to a high level, so that all jobs will be processed in a maximum time.
This class is virtual, and it is designed for inheritance: Using software has to derive custom types that provide the custom functionality.
Jobs are represented with the protected type Job.
Derived custom types need to duly fulfill a certain contract of interaction. In short, the following rules are given:
The class inherits type Thread as a protected base type. With that, the conventional methods to start and stop a thread are hidden.
Starting and stopping a DedicatedWorker is instead performed by adding, respectively removing an instance of this type to singleton class DWManager.
This type implements abstract interface Triggered. If this is considered useful by a derived type, then three things have to be performed:
Definition at line 205 of file dedicatedworker.hpp.
#include <dedicatedworker.hpp>
Inner Type Index: | |
struct | JobDeleter |
The job sent by method DeleteJobDeferred. More... | |
struct | JobStop |
The stop job sent by method ScheduleStop. More... | |
struct | JobTrigger |
struct | QueueElement |
Container element of the queue. More... | |
Public Field Index: | |
int | DbgMaxQueuelength |
Public Method Index: | |
DedicatedWorker (const alib::String &threadName) | |
~DedicatedWorker () override | |
Destructor. | |
void | DeleteJob (Job &job) |
void | DeleteJobDeferred (Job &job) |
virtual const CString | GetName () const |
int | Load () const |
void | ScheduleStop (Priority priority) |
bool | StopIsExecuted () |
bool | StopIsScheduled () |
Protected Field Index: | |
int | length |
The current number of jobs in the queue. | |
DWManager & | manager |
needed as we inherit TCondition | |
List< HeapAllocator, QueueElement > | queue |
alib::Ticks | statLastJobExecution |
bool | stopJobExecuted = false |
Flag which is set when the stop-job was executed. | |
bool | stopJobPushed = false |
Flag which is set when the stop-job was scheduled. | |
Ticks::Duration | triggerDuration = Ticks::Duration::FromSeconds(1) |
Protected Field Index: inherited from Thread | |
std::thread * | c11Thread =nullptr |
The internal C++ thread object. | |
ThreadID | id =0 |
The id of the thread. | |
AString | name |
The name of the thread. | |
std::thread::id | nativeID |
The internal C++ thread id. | |
Runnable * | runnable =nullptr |
The runnable to execute. | |
State | state = State::Unstarted |
Internal flag to indicate if the thread is alive. | |
Protected Field Index: inherited from Triggered | |
const String | Name |
Protected Field Index: inherited from TCondition< TDerived > | |
std::condition_variable | conditionVariable |
The condition variable used for blocking and notification. | |
DbgConditionAsserter | Dbg |
The debug tool instance. | |
std::mutex | mutex |
The mutex used for locking this instance. | |
Protected Method Index: | |
bool | isConditionMet () |
std::pair< Job *, bool > | pop () |
virtual bool | process (Job &vjob) |
ALIB_API void | pushAndRelease (QueueElement &&jobInfo) |
virtual ALIB_API void | Run () override |
template<typename TJob , typename... TArgs> | |
TJob & | Schedule (Priority priority, TArgs &&... args) |
template<typename TJob , typename... TArgs> | |
TJob & | schedule (Priority priority, bool keepJob, TArgs &&... args) |
template<typename TJob , typename... TArgs> | |
void | ScheduleVoid (Priority priority, TArgs &&... args) |
virtual void | trigger () override |
virtual Ticks::Duration | triggerPeriod () override |
Protected Method Index: inherited from Thread | |
ALIB_API | Thread (const String &pName=EMPTY_STRING) |
Thread (const Thread &)=delete | |
Deleted copy constructor. | |
ALIB_API | Thread (Runnable *target, const String &pName=EMPTY_STRING) |
virtual ALIB_API | ~Thread () override |
ThreadID | GetID () const |
std::thread::id | GetNativeID () const |
State | GetState () |
bool | IsAlive () |
virtual ALIB_API void | Join () |
virtual void | SetName (const String &newName) |
virtual ALIB_API void | Start () |
Protected Method Index: inherited from Runnable | |
virtual | ~Runnable () |
Virtual destructor. | |
Protected Method Index: inherited from Triggered | |
Triggered (const String &pName) | |
virtual | ~Triggered () |
Virtual empty destructor. Needed with any virtual class. | |
Protected Method Index: inherited from TCondition< TDerived > | |
TCondition (const String &dbgName) | |
void | Acquire (ALIB_DBG_TAKE_CI) |
TDerived & | cast () |
void | Release (ALIB_DBG_TAKE_CI) |
void | ReleaseAndNotify (ALIB_DBG_TAKE_CI) |
void | ReleaseAndNotifyAll (ALIB_DBG_TAKE_CI) |
void | WaitForNotification (ALIB_DBG_TAKE_CI) |
void | WaitForNotification (const Ticks &wakeUpTime, const CallerInfo &ci) |
void | WaitForNotification (const Ticks::Duration &maxWaitTimeSpan, const CallerInfo &ci) |
void | WaitForNotification (const Ticks::Duration::TDuration &maxWaitTimeSpan, const CallerInfo &ci) |
Additional Inherited Members | |
Protected Type Index: inherited from Thread | |
enum class | State { Unstarted = 0 , Started = 1 , Running = 2 , Done = 3 , Terminated = 4 } |
Protected Static Method Index: inherited from Thread | |
static ALIB_API Thread * | Get (std::thread::id nativeID) |
static Thread * | GetCurrent () |
static ALIB_API Thread * | GetMain () |
static void | Sleep (const Ticks::Duration &duration) |
static void | Sleep (const Ticks::Duration::TDuration &duration) |
static void | SleepMicros (int64_t microseconds) |
static void | SleepMillis (int milliseconds) |
static void | SleepNanos (int64_t nanoseconds) |
static void | SleepUntil (const Ticks &time) |
static void | YieldToSystem () |
|
friend |
Type alias in namespace alib.
Definition at line 209 of file dedicatedworker.hpp.
|
friend |
Definition at line 209 of file dedicatedworker.hpp.
|
friend |
Definition at line 209 of file dedicatedworker.hpp.
int DbgMaxQueuelength |
The maximum number of jobs in the queue at any time. Available only with debug-compilations.
Definition at line 423 of file dedicatedworker.hpp.
|
protected |
The current number of jobs in the queue.
Definition at line 259 of file dedicatedworker.hpp.
|
protected |
needed as we inherit TCondition
Reference to DWManager instance.
Definition at line 221 of file dedicatedworker.hpp.
|
protected |
The queue of jobs. This is a simple list, instead of a 'real' priority queue. This design decision was taken because there should never be too many jobs queued and the naive iteration is more efficient than using a 'real' priority queue type.
Definition at line 256 of file dedicatedworker.hpp.
|
protected |
Statistical information: Point in time of last job execution. In case no job was executed, yet, this is the creation time of the object.
Definition at line 225 of file dedicatedworker.hpp.
|
protected |
Flag which is set when the stop-job was executed.
Definition at line 231 of file dedicatedworker.hpp.
|
protected |
Flag which is set when the stop-job was scheduled.
Definition at line 228 of file dedicatedworker.hpp.
|
protected |
If this DedicatedWorker is (in addition) attached to a DWManager as a triggered object, this duration is returned by overridden method Triggered::triggerPeriod to determine the interval between scheduling two trigger jobs.
Defaults to one second, which usually is changed by a derived type.
Definition at line 238 of file dedicatedworker.hpp.
|
inline |
Constructor taking a thread name that is passed to parent class Thread.
threadName | The name of this thread. The string's data has to survive this threads' lifespan. |
Definition at line 433 of file dedicatedworker.hpp.
|
inlineoverride |
Destructor.
Definition at line 444 of file dedicatedworker.hpp.
|
inline |
Deletes a data object previously received via method schedule, one of its siblings, or scheduling methods of derived types.
The latter might have names that do not contain the term "schedule" but still internally create and return data objects. Any object returned needs to be deleted.
This can be cumbersome in case the calling thread is just not interested in the result. For this case, the alternative method # DeleteJobDeferred is given.
job | The job returned when scheduling a command. |
Definition at line 498 of file dedicatedworker.hpp.
|
inline |
Same as DeleteJob but schedules the deletion to be performed. Scheduling is done with Priority::DeferredDeletion.
This assures that the job deletion is performed after the job execution.
This method is useful when the thread that schedules a job with this DedicatedWorker is not interested in the result, i.e., does not perform an asynchronous wait. Custom jobs where this is rather usually the case, should be exposed in two versions, one that returns a result and another that does not. With the second, the derived DedicatedWorker would delete the given shared data with processing the job. If this is offered, then this method does not need to be called (and cannot be called because the caller does not receive any data).
job | The job object to delete. |
Definition at line 519 of file dedicatedworker.hpp.
|
inlinevirtual |
Returns the name of the thread. An ALib thread can have any name that is given to it and such name can be changed any time. In fact, names are for debugging and logging purposes only.
Reimplemented from Thread.
Definition at line 225 of file thread.hpp.
|
inlineprotected |
Mandatory method needed and invoked by templated base type TCondition.
true
if field queue is not empty, false
otherwise. Definition at line 263 of file dedicatedworker.hpp.
|
inline |
Returns the current number of jobs in the queue.
Definition at line 457 of file dedicatedworker.hpp.
|
protected |
Moves the job of highest priority out of the queue. Blocks the thread until a job is available.
Definition at line 237 of file dedicatedworker.cpp.
|
inlineprotectedvirtual |
This virtual method is called during thread execution (method Run) for each job extracted from the internal job-queue.
While this default-implementation is empty, internally, before a call to this method, the following jobs are already detected and processed:
A derived type may decide to call this parent method before or after checking for its own jobs. If the call returns true
, then the job was processed.
It is also allowed to implement a custom processing, for example, with JobStop and thus not to call this version for the internal jobs covered by the custom version.
It is important that overridden versions do return the true
if the job was processed and false
if not.
vjob | The job to process. |
true
if the job was processed, false
if not. Definition at line 417 of file dedicatedworker.hpp.
|
protected |
Pushes the given jobInfo into the priority queue that this class implements. When invoked, the thread-manager as well as this instance are both locked.
jobInfo | The job, the priority, and a flag if this job is to be deleted automatically. |
Definition at line 209 of file dedicatedworker.cpp.
|
overrideprotectedvirtual |
This implementation of method Thread::Run constitutes a very simple loop that waits for jobs in the queue and passes them to likewise virtual method process. The (only) condition to continuing the loop is that the flag stopJobExecuted is false
. This flag will be set by the internal job type JobStop which is pushed with the method ScheduleStop.
Reimplemented from Thread.
Definition at line 259 of file dedicatedworker.cpp.
|
inlineprotected |
Pushes a custom job into the priority queue.
The job is returned to the caller to be able to await results. It is the responsibility of the caller to pass the job to either method DeleteJob or DeleteJobDeferred for disposal.
TJob | The job type to create and schedule. |
TArgs | Types of the variadic arguments args that construct TJob. |
priority | The priority of the job. |
args | Variadic arguments forwarded to the constructor of TJob. |
Definition at line 357 of file dedicatedworker.hpp.
|
inlinenodiscardprotected |
Pushes a custom job into the priority queue.
This method is protected because derived types should provide dedicated "speaking" interfaces for scheduling jobs. Those are typically short inline methods, which/ are optimized out by C++ compilers.
TJob | The job type as well as the job's shared data type. |
TArgs | Types of the variadic arguments args that construct TJob. |
priority | The priority of the job. |
keepJob | Denotes whether the job should be deleted after execution or not. |
args | Variadic arguments forwarded to the constructor of TJob. |
Definition at line 326 of file dedicatedworker.hpp.
|
inline |
Schedules a stop job into this thread's job queue. When this job is processed from the queue, this thread will exit.
priority | The priority of the job. Use Lowest if it is assured that no other jobs will be pushed. |
Definition at line 475 of file dedicatedworker.hpp.
|
inlineprotected |
Pushes a custom job into the priority queue. In contrast to the sibling method Schedule, the job is not returned by this method. Instead, it is scheduled for automatic disposal after execution.
TJob | The job type to create and schedule. |
TArgs | Types of the variadic arguments args that construct TJob. |
priority | The priority of the job. |
args | Variadic arguments forwarded to the constructor of TJob. |
Definition at line 368 of file dedicatedworker.hpp.
|
inline |
Returns the state of the internal flag, which is set with the execution of ScheduleStop.
true
, if the ScheduleStop was processed, false
otherwise. Definition at line 465 of file dedicatedworker.hpp.
|
inline |
Returns the state of the internal flag, which is set with the invocation of ScheduleStop.
true
, if the ScheduleStop was called, false
otherwise. Definition at line 461 of file dedicatedworker.hpp.
|
inlineoverrideprotectedvirtual |
Schedules JobTrigger need to implement this function and perform their trigger actions here.
Implements Triggered.
Definition at line 383 of file dedicatedworker.hpp.
|
inlineoverrideprotectedvirtual |
Return the sleep time, between two trigger events. Precisely, this method is called after trigger has been executed and defines the next sleep time.
Implements Triggered.
Definition at line 379 of file dedicatedworker.hpp.