ThreadWeaver 5.109.0
Public Member Functions | List of all members
ThreadWeaver::DestructedState

DestructedState is only active after the thread have been destroyed by the destructor, but before superclass destructors have finished. More...

#include <destructedstate.h>

Public Member Functions

 DestructedState (QueueSignals *weaver)
 
JobPointer applyForWork (Thread *th, bool wasBusy) override
 
int currentNumberOfThreads () const override
 Returns the current number of threads in the inventory.
 
void dequeue () override
 Remove all queued jobs.
 
bool dequeue (const JobPointer &job) override
 Remove a job from the queue.
 
void enqueue (const QVector< JobPointer > &job) override
 Queue a vector of jobs.
 
void finish () override
 Finish all queued operations, then return.
 
bool isEmpty () const override
 Is the queue empty? The queue is empty if no more jobs are queued.
 
bool isIdle () const override
 Is the weaver idle? The weaver is idle if no jobs are queued and no jobs are processed by the threads.
 
int maximumNumberOfThreads () const override
 Get the maximum number of threads this Weaver may start.
 
int queueLength () const override
 Returns the number of pending jobs.
 
void requestAbort () override
 Request aborts of the currently executed jobs.
 
void resume () override
 Resume job queueing.
 
void setMaximumNumberOfThreads (int cap) override
 Set the maximum number of threads this Weaver object may start.
 
void shutDown () override
 Shut down the queue.
 
StateId stateId () const override
 The state Id.
 
void suspend () override
 Suspend job execution.
 
void waitForAvailableJob (Thread *th) override
 
const Weaverweaver () const override
 
Weaverweaver () override
 The Weaver interface this state handles.
 
- Public Member Functions inherited from ThreadWeaver::WeaverImplState
 WeaverImplState (QueueSignals *weaver)
 
int currentNumberOfThreads () const override
 Returns the current number of threads in the inventory.
 
void dequeue () override
 Dequeue all jobs.
 
bool dequeue (const JobPointer &job) override
 Dequeue a job.
 
void enqueue (const QVector< JobPointer > &jobs) override
 Enqueue a job.
 
void finish () override
 Finish all queued jobs.
 
bool isEmpty () const override
 Are no more jobs queued?
 
bool isIdle () const override
 Are all threads waiting?
 
int maximumNumberOfThreads () const override
 Get the maximum number of threads this Weaver may start.
 
int queueLength () const override
 How many jobs are currently queued?
 
void requestAbort () override
 Request abort for all queued and currently executed jobs.
 
void reschedule () override
 Reschedule jobs to threads.
 
void setMaximumNumberOfThreads (int cap) override
 Set the maximum number of threads this Weaver object may start.
 
void shutDown () override
 Shut down the queue.
 
const Statestate () const override
 Return the state of the weaver object.
 
void waitForAvailableJob (Thread *th) override
 Wait (by suspending the calling thread) until a job becomes available.
 
- Public Member Functions inherited from ThreadWeaver::State
 State (QueueSignals *weaver)
 Default constructor.
 
 ~State () override
 Destructor.
 
virtual void activated ()
 The state has been changed so that this object is responsible for state handling.
 
virtual StateId stateId () const =0
 The state Id.
 
QString stateName () const
 The ID of the current state.
 
virtual int currentNumberOfThreads () const =0
 Returns the current number of threads in the inventory.
 
virtual void dequeue ()=0
 Remove all queued jobs.
 
virtual bool dequeue (const JobPointer &job)=0
 Remove a job from the queue.
 
virtual void enqueue (const QVector< JobPointer > &jobs)=0
 Queue a vector of jobs.
 
virtual void finish ()=0
 Finish all queued operations, then return.
 
virtual bool isEmpty () const =0
 Is the queue empty? The queue is empty if no more jobs are queued.
 
virtual bool isIdle () const =0
 Is the weaver idle? The weaver is idle if no jobs are queued and no jobs are processed by the threads.
 
virtual int maximumNumberOfThreads () const =0
 Get the maximum number of threads this Weaver may start.
 
virtual int queueLength () const =0
 Returns the number of pending jobs.
 
virtual void requestAbort ()=0
 Request aborts of the currently executed jobs.
 
virtual void reschedule ()=0
 Reschedule the jobs in the queue.
 
virtual void resume ()=0
 Resume job queueing.
 
virtual void setMaximumNumberOfThreads (int cap)=0
 Set the maximum number of threads this Weaver object may start.
 
virtual void shutDown ()=0
 Shut down the queue.
 
virtual const Statestate () const =0
 Return the state of the weaver object.
 
virtual void suspend ()=0
 Suspend job execution.
 

Additional Inherited Members

- Protected Member Functions inherited from ThreadWeaver::WeaverImplState
const Weaverweaver () const override
 
Weaverweaver () override
 Provide correct return type for WeaverImpl states.
 
virtual QueueInterfaceweaver ()
 The Weaver interface this state handles.
 
virtual const QueueInterfaceweaver () const
 

Detailed Description

DestructedState is only active after the thread have been destroyed by the destructor, but before superclass destructors have finished.

Member Function Documentation

◆ currentNumberOfThreads()

int ThreadWeaver::DestructedState::currentNumberOfThreads ( ) const
overridevirtual

Returns the current number of threads in the inventory.

Implements ThreadWeaver::QueueInterface.

◆ dequeue() [1/2]

void ThreadWeaver::DestructedState::dequeue ( )
overridevirtual

Remove all queued jobs.

All waiting jobs will be dequeued. The semantics are the same as for dequeue(JobInterface).

See also
dequeue(JobInterface)

Implements ThreadWeaver::QueueInterface.

◆ dequeue() [2/2]

bool ThreadWeaver::DestructedState::dequeue ( const JobPointer &  job)
overridevirtual

Remove a job from the queue.

If the job was queued but not started so far, it is removed from the queue.

You can always call dequeue, it will return true if the job was dequeued. However if the job is not in the queue anymore, it is already being executed, it is too late to dequeue, and dequeue will return false. The return value is thread-safe - if true is returned, the job was still waiting, and has been dequeued. If not, the job was not waiting in the queue.

Modifying queued jobs is best done on a suspended queue. Often, for example at the end of an application, it is sufficient to dequeue all jobs (which leaves only the ones mid-air in threads), call finish (that will wait for all the mid air jobs to complete), and then exit. Without dequeue(), all jobs in the queue would be executed during finish().

See also
requestAbort for aborting jobs during execution
Returns
true if the job was waiting and has been dequeued
false if the job was not found waiting in the queue

Implements ThreadWeaver::QueueInterface.

◆ enqueue()

void ThreadWeaver::DestructedState::enqueue ( const QVector< JobPointer > &  jobs)
overridevirtual

Queue a vector of jobs.

It depends on the state if execution of the job will be attempted immediately. In suspended state, jobs can be added to the queue, but the threads remain suspended. In WorkongHard state, an idle thread may immediately execute the job, or it might be queued if all threads are busy.

JobPointer is a shared pointer. This means the object pointed to will be deleted if this object is the last remaining reference to it. Keep a JobPointer to the job to avoid automatic deletion.

Implements ThreadWeaver::QueueInterface.

◆ finish()

void ThreadWeaver::DestructedState::finish ( )
overridevirtual

Finish all queued operations, then return.

This method is used in imperative (not event driven) programs that cannot react on events to have the controlling (main) thread wait wait for the jobs to finish. The call will block the calling thread and return when all queued jobs have been processed.

Warning: This will suspend your thread! Warning: If one of your jobs enters an infinite loop, this will never return!

Implements ThreadWeaver::QueueInterface.

◆ isEmpty()

bool ThreadWeaver::DestructedState::isEmpty ( ) const
overridevirtual

Is the queue empty? The queue is empty if no more jobs are queued.

Implements ThreadWeaver::QueueInterface.

◆ isIdle()

bool ThreadWeaver::DestructedState::isIdle ( ) const
overridevirtual

Is the weaver idle? The weaver is idle if no jobs are queued and no jobs are processed by the threads.

Implements ThreadWeaver::QueueInterface.

◆ maximumNumberOfThreads()

int ThreadWeaver::DestructedState::maximumNumberOfThreads ( ) const
overridevirtual

Get the maximum number of threads this Weaver may start.

Implements ThreadWeaver::QueueInterface.

◆ queueLength()

int ThreadWeaver::DestructedState::queueLength ( ) const
overridevirtual

Returns the number of pending jobs.

This will return the number of queued jobs. Jobs that are currently being executed are not part of the queue. All jobs in the queue are waiting to be executed.

Implements ThreadWeaver::QueueInterface.

◆ requestAbort()

void ThreadWeaver::DestructedState::requestAbort ( )
overridevirtual

Request aborts of the currently executed jobs.

It is important to understand that aborts are requested, but cannot be guaranteed, as not all Job classes support it. It is up to the application to decide if and how job aborts are necessary.

Implements ThreadWeaver::QueueInterface.

◆ resume()

void ThreadWeaver::DestructedState::resume ( )
overridevirtual

Resume job queueing.

See also
suspend

Implements ThreadWeaver::QueueInterface.

◆ setMaximumNumberOfThreads()

void ThreadWeaver::DestructedState::setMaximumNumberOfThreads ( int  cap)
overridevirtual

Set the maximum number of threads this Weaver object may start.

Implements ThreadWeaver::QueueInterface.

◆ shutDown()

void ThreadWeaver::DestructedState::shutDown ( )
overridevirtual

Shut down the queue.

Tells all threads to exit, and changes to Destructed state. It is safe to destroy the queue once this method returns.

Implements ThreadWeaver::QueueInterface.

◆ stateId()

StateId ThreadWeaver::DestructedState::stateId ( ) const
overridevirtual

The state Id.

Implements ThreadWeaver::State.

◆ suspend()

void ThreadWeaver::DestructedState::suspend ( )
overridevirtual

Suspend job execution.

When suspending, all threads are allowed to finish the currently assigned job but will not receive a new assignment. When all threads are done processing the assigned job, the signal suspended will() be emitted. If you call suspend() and there are no jobs left to be done, you will immediately receive the suspended() signal.

Implements ThreadWeaver::QueueInterface.

◆ weaver() [1/2]

const Weaver * ThreadWeaver::DestructedState::weaver ( ) const
overridevirtual

Reimplemented from ThreadWeaver::State.

◆ weaver() [2/2]

Weaver * ThreadWeaver::DestructedState::weaver ( )
overridevirtual

The Weaver interface this state handles.

Reimplemented from ThreadWeaver::State.