29 :
Thread { options.threadName, options.threadStackSizeBytes },
38 if (! pool.runNextJob (*
this))
80 listeners.add (listener);
85 listeners.remove (listener);
91 return t->currentJob.load();
100 jassert (options.numberOfThreads > 0);
102 for (
int i =
jmax (1, options.numberOfThreads); --i >= 0;)
105 for (
auto* t : threads)
106 t->startThread (options.desiredThreadPriority);
110 size_t threadStackSizeBytes,
113 .withThreadStackSizeBytes (threadStackSizeBytes)
114 .withDesiredThreadPriority (desiredThreadPriority) }
124void ThreadPool::stopThreads()
126 for (
auto* t : threads)
127 t->signalThreadShouldExit();
129 for (
auto* t : threads)
136 jassert (job->pool ==
nullptr);
138 if (job->pool ==
nullptr)
141 job->shouldStop =
false;
142 job->isActive =
false;
143 job->shouldBeDeleted = deleteJobWhenFinished;
150 for (
auto* t : threads)
160 JobStatus runJob()
override {
return job(); }
165 addJob (
new LambdaJobWrapper (jobToRun),
true);
173 JobStatus runJob()
override { job();
return ThreadPoolJob::jobHasFinished; }
178 addJob (
new LambdaJobWrapper (std::move (jobToRun)),
true);
189 return threads.size();
207 return jobs.contains (
const_cast<ThreadPoolJob*
> (job)) && job->isActive;
214 auto index = jobs.indexOf (
const_cast<ThreadPoolJob*
> (job));
216 if (index > 0 && ! job->isActive)
217 jobs.move (index, 0);
231 jobFinishedSignal.
wait (2);
240 bool dontWait =
true;
247 if (jobs.contains (job))
251 if (interruptIfRunning)
258 jobs.removeFirstMatchingValue (job);
259 addToDeleteList (deletionList, job);
278 for (
int i = jobs.size(); --i >= 0;)
280 auto* job = jobs.getUnchecked (i);
282 if (selectedJobsToRemove ==
nullptr || selectedJobsToRemove->
isJobSuitable (job))
286 jobsToWaitFor.
add (job);
288 if (interruptRunningJobs)
289 job->signalJobShouldExit();
294 addToDeleteList (deletionList, job);
305 for (
int i = jobsToWaitFor.
size(); --i >= 0;)
313 if (jobsToWaitFor.
size() == 0)
319 jobFinishedSignal.
wait (20);
330 for (
auto* job : jobs)
331 if (job->isActive || ! onlyReturnActiveJobs)
332 s.
add (job->getJobName());
344 for (
int i = 0; i < jobs.size(); ++i)
346 if (
auto* job = jobs[i])
353 addToDeleteList (deletionList, job);
358 job->isActive =
true;
368bool ThreadPool::runNextJob (ThreadPoolThread& thread)
370 if (
auto* job = pickNextJobToRun())
373 thread.currentJob = job;
377 result = job->runJob();
384 thread.currentJob =
nullptr;
386 OwnedArray<ThreadPoolJob> deletionList;
391 if (jobs.contains (job))
393 job->isActive =
false;
397 jobs.removeFirstMatchingValue (job);
398 addToDeleteList (deletionList, job);
400 jobFinishedSignal.
signal();
405 jobs.move (jobs.indexOf (job), -1);
416void ThreadPool::addToDeleteList (OwnedArray<ThreadPoolJob>& deletionList, ThreadPoolJob* job)
const
418 job->shouldStop =
true;
421 if (job->shouldBeDeleted)
422 deletionList.add (job);
Holds a resizable array of primitive or copy-by-value objects.
ElementType getUnchecked(int index) const
Returns one of the elements in the array, without checking the index passed in.
int size() const noexcept
Returns the current number of elements in the array.
void remove(int indexToRemove)
Removes an element from the array.
void add(const ElementType &newElement)
Appends a new element at the end of the array.
Automatically locks and unlocks a mutex object.
An array designed for holding objects.
A special array for holding a list of strings.
void add(String stringToAdd)
Appends a string at the end of the array.
A task that is executed by a ThreadPool object.
void signalJobShouldExit()
Calling this will cause the shouldExit() method to return true, and the job should (if it's been impl...
JobStatus
These are the values that can be returned by the runJob() method.
@ 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.
void setJobName(const String &newName)
Changes the job's name.
String getJobName() const
Returns the name of this job.
void addListener(Thread::Listener *)
Add a listener to this thread job which will receive a callback when signalJobShouldExit was called o...
virtual ~ThreadPoolJob()
Destructor.
static ThreadPoolJob * getCurrentThreadPoolJob()
If the calling thread is being invoked inside a runJob() method, this will return the ThreadPoolJob t...
void removeListener(Thread::Listener *)
Removes a listener added with addListener.
ThreadPoolJob(const String &name)
Creates a thread pool job object.
A callback class used when you need to select which ThreadPoolJob objects are suitable for some kind ...
virtual bool isJobSuitable(ThreadPoolJob *job)=0
Should return true if the specified thread matches your criteria for whatever operation that this obj...
A set of threads that will run a list of jobs.
void moveJobToFront(const ThreadPoolJob *jobToMove) noexcept
If the given job is in the queue, this will move it to the front so that it is the next one to be exe...
void addJob(ThreadPoolJob *job, bool deleteJobWhenFinished)
Adds a job to the queue.
int getNumThreads() const noexcept
Returns the number of threads assigned to this thread pool.
ThreadPoolJob * getJob(int index) const noexcept
Returns one of the jobs in the queue.
int getNumJobs() const noexcept
Returns the number of jobs currently running or queued.
bool removeAllJobs(bool interruptRunningJobs, int timeOutMilliseconds, JobSelector *selectedJobsToRemove=nullptr)
Tries to remove all jobs from the pool.
StringArray getNamesOfAllJobs(bool onlyReturnActiveJobs) const
Returns a list of the names of all the jobs currently running or queued.
bool isJobRunning(const ThreadPoolJob *job) const noexcept
Returns true if the given job is currently being run by a thread.
bool removeJob(ThreadPoolJob *job, bool interruptIfRunning, int timeOutMilliseconds)
Tries to remove a job from the pool.
bool contains(const ThreadPoolJob *job) const noexcept
Returns true if the given job is currently queued or running.
bool waitForJobToFinish(const ThreadPoolJob *job, int timeOutMilliseconds) const
Waits until a job has finished running and has been removed from the pool.
ThreadPool()
Creates a thread pool based using the default arguments provided by ThreadPoolOptions.
Used to receive callbacks for thread exit calls.
virtual void exitSignalSent()=0
Called if Thread::signalThreadShouldExit was called.
static Thread *JUCE_CALLTYPE getCurrentThread()
Finds the thread object that is currently running.
bool wait(double timeOutMilliseconds) const
Suspends the execution of this thread until either the specified timeout period has elapsed,...
bool threadShouldExit() const
Checks whether the thread has been told to stop running.
Priority
The different runtime priorities of non-realtime threads.
static uint32 getMillisecondCounter() noexcept
Returns the number of millisecs since a fixed event (usually system startup).
void signal() const
Wakes up any threads that are currently waiting on this object.
bool wait(double timeOutMilliseconds=-1.0) const
Suspends the calling thread until the event has been signalled.
CriticalSection::ScopedLockType ScopedLock
Automatically locks and unlocks a CriticalSection object.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
unsigned int uint32
A platform-independent 32-bit unsigned integer type.
A set of threads that will run a list of jobs.
void run() override
Must be implemented to perform the thread's actual code.