JUCE  v5.4.1-191-g0ab5e696f
JUCE API
Looking for a senior C++ dev?
I'm looking for work. Hire me!
juce::ThreadPoolJob Class Referenceabstract

A task that is executed by a ThreadPool object. More...

#include <juce_ThreadPool.h>

Collaboration diagram for juce::ThreadPoolJob:

Public Types

enum  JobStatus {
  jobHasFinished = 0,
  jobNeedsRunningAgain
}
 These are the values that can be returned by the runJob() method. More...
 

Public Member Functions

 ThreadPoolJob (const String &name)
 Creates a thread pool job object. More...
 
virtual ~ThreadPoolJob ()
 Destructor. More...
 
void addListener (Thread::Listener *)
 Add a listener to this thread job which will receive a callback when signalJobShouldExit was called on this thread job. More...
 
String getJobName () const
 Returns the name of this job. More...
 
bool isRunning () const noexcept
 Returns true if this job is currently running its runJob() method. More...
 
void removeListener (Thread::Listener *)
 Removes a listener added with addListener. More...
 
virtual JobStatus runJob ()=0
 Peforms the actual work that this job needs to do. More...
 
void setJobName (const String &newName)
 Changes the job's name. More...
 
bool shouldExit () const noexcept
 Returns true if something is trying to interrupt this job and make it stop. More...
 
void signalJobShouldExit ()
 Calling this will cause the shouldExit() method to return true, and the job should (if it's been implemented correctly) stop as soon as possible. More...
 

Static Public Member Functions

static ThreadPoolJobgetCurrentThreadPoolJob ()
 If the calling thread is being invoked inside a runJob() method, this will return the ThreadPoolJob that it belongs to. More...
 

Private Attributes

std::atomic< boolisActive { false }
 
String jobName
 
ListenerList< Thread::Listener, Array< Thread::Listener *, CriticalSection > > listeners
 
ThreadPoolpool = nullptr
 
std::atomic< boolshouldBeDeleted { false }
 
std::atomic< boolshouldStop { false }
 

Friends

class ThreadPool
 

Detailed Description

A task that is executed by a ThreadPool object.

A ThreadPool keeps a list of ThreadPoolJob objects which are executed by its threads.

The runJob() method needs to be implemented to do the task, and if the code that does the work takes a significant time to run, it must keep checking the shouldExit() method to see if something is trying to interrupt the job. If shouldExit() returns true, the runJob() method must return immediately.

See also
ThreadPool, Thread

{Core}

Member Enumeration Documentation

◆ JobStatus

These are the values that can be returned by the runJob() method.

Enumerator
jobHasFinished 

indicates that the job has finished and can be removed from the pool.

jobNeedsRunningAgain 

indicates that the job would like to be called again when a thread is free.

Constructor & Destructor Documentation

◆ ThreadPoolJob()

juce::ThreadPoolJob::ThreadPoolJob ( const String name)
explicit

Creates a thread pool job object.

After creating your job, add it to a thread pool with ThreadPool::addJob().

◆ ~ThreadPoolJob()

virtual juce::ThreadPoolJob::~ThreadPoolJob ( )
virtual

Destructor.

Member Function Documentation

◆ addListener()

void juce::ThreadPoolJob::addListener ( Thread::Listener )

Add a listener to this thread job which will receive a callback when signalJobShouldExit was called on this thread job.

See also
signalJobShouldExit, removeListener

◆ getCurrentThreadPoolJob()

static ThreadPoolJob* juce::ThreadPoolJob::getCurrentThreadPoolJob ( )
static

If the calling thread is being invoked inside a runJob() method, this will return the ThreadPoolJob that it belongs to.

◆ getJobName()

String juce::ThreadPoolJob::getJobName ( ) const

Returns the name of this job.

See also
setJobName

◆ isRunning()

bool juce::ThreadPoolJob::isRunning ( ) const
inlinenoexcept

Returns true if this job is currently running its runJob() method.

◆ removeListener()

void juce::ThreadPoolJob::removeListener ( Thread::Listener )

Removes a listener added with addListener.

◆ runJob()

virtual JobStatus juce::ThreadPoolJob::runJob ( )
pure virtual

Peforms the actual work that this job needs to do.

Your subclass must implement this method, in which is does its work.

If the code in this method takes a significant time to run, it must repeatedly check the shouldExit() method to see if something is trying to interrupt the job. If shouldExit() ever returns true, the runJob() method must return immediately.

If this method returns jobHasFinished, then the job will be removed from the pool immediately. If it returns jobNeedsRunningAgain, then the job will be left in the pool and will get a chance to run again as soon as a thread is free.

See also
shouldExit()

◆ setJobName()

void juce::ThreadPoolJob::setJobName ( const String newName)

Changes the job's name.

See also
getJobName

◆ shouldExit()

bool juce::ThreadPoolJob::shouldExit ( ) const
inlinenoexcept

Returns true if something is trying to interrupt this job and make it stop.

Your runJob() method must call this whenever it gets a chance, and if it ever returns true, the runJob() method must return immediately.

See also
signalJobShouldExit()

◆ signalJobShouldExit()

void juce::ThreadPoolJob::signalJobShouldExit ( )

Calling this will cause the shouldExit() method to return true, and the job should (if it's been implemented correctly) stop as soon as possible.

See also
shouldExit()

Friends And Related Function Documentation

◆ ThreadPool

friend class ThreadPool
friend

Member Data Documentation

◆ isActive

std::atomic<bool> juce::ThreadPoolJob::isActive { false }
private

◆ jobName

String juce::ThreadPoolJob::jobName
private

◆ listeners

ListenerList<Thread::Listener, Array<Thread::Listener*, CriticalSection> > juce::ThreadPoolJob::listeners
private

◆ pool

ThreadPool* juce::ThreadPoolJob::pool = nullptr
private

◆ shouldBeDeleted

std::atomic<bool> juce::ThreadPoolJob::shouldBeDeleted { false }
private

◆ shouldStop

std::atomic<bool> juce::ThreadPoolJob::shouldStop { false }
private

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