JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_ThreadPool.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26class ThreadPool;
27
28//==============================================================================
44class JUCE_API ThreadPoolJob
45{
46public:
47 //==============================================================================
51 explicit ThreadPoolJob (const String& name);
52
54 virtual ~ThreadPoolJob();
55
56 //==============================================================================
60 String getJobName() const;
61
65 void setJobName (const String& newName);
66
67 //==============================================================================
71 {
72 jobHasFinished = 0,
75 jobNeedsRunningAgain
77 };
78
93 virtual JobStatus runJob() = 0;
94
95
96 //==============================================================================
98 bool isRunning() const noexcept { return isActive; }
99
107 bool shouldExit() const noexcept { return shouldStop; }
108
114 void signalJobShouldExit();
115
121 void addListener (Thread::Listener*);
122
124 void removeListener (Thread::Listener*);
125
126 //==============================================================================
130 static ThreadPoolJob* getCurrentThreadPoolJob();
131
132 //==============================================================================
133private:
134 friend class ThreadPool;
135 String jobName;
136 ThreadPool* pool = nullptr;
137 std::atomic<bool> shouldStop { false }, isActive { false }, shouldBeDeleted { false };
138 ListenerList<Thread::Listener, Array<Thread::Listener*, CriticalSection>> listeners;
139
141};
142
143//==============================================================================
155{
158 {
159 return withMember (*this, &ThreadPoolOptions::threadName, newThreadName);
160 }
161
166 {
167 return withMember (*this, &ThreadPoolOptions::numberOfThreads, newNumberOfThreads);
168 }
169
172 {
173 return withMember (*this, &ThreadPoolOptions::threadStackSizeBytes, newThreadStackSizeBytes);
174 }
175
178 {
179 return withMember (*this, &ThreadPoolOptions::desiredThreadPriority, newDesiredThreadPriority);
180 }
181
182 String threadName { "Pool" };
183 int numberOfThreads { SystemStats::getNumCpus() };
184 size_t threadStackSizeBytes { Thread::osDefaultStackSize };
185 Thread::Priority desiredThreadPriority { Thread::Priority::normal };
186};
187
188
189//==============================================================================
200class JUCE_API ThreadPool
201{
202public:
204
205 //==============================================================================
211 explicit ThreadPool (const Options& options);
212
221
232 ThreadPool (int numberOfThreads,
233 size_t threadStackSizeBytes = Thread::osDefaultStackSize,
234 Thread::Priority desiredThreadPriority = Thread::Priority::normal);
235
242 ~ThreadPool();
243
244 //==============================================================================
249 class JUCE_API JobSelector
250 {
251 public:
252 virtual ~JobSelector() = default;
253
259 virtual bool isJobSuitable (ThreadPoolJob* job) = 0;
260 };
261
262 //==============================================================================
278 void addJob (ThreadPoolJob* job,
279 bool deleteJobWhenFinished);
280
284 void addJob (std::function<ThreadPoolJob::JobStatus()> job);
285
289 void addJob (std::function<void()> job);
290
308 bool removeJob (ThreadPoolJob* job,
309 bool interruptIfRunning,
310 int timeOutMilliseconds);
311
323 bool removeAllJobs (bool interruptRunningJobs,
324 int timeOutMilliseconds,
325 JobSelector* selectedJobsToRemove = nullptr);
326
328 int getNumJobs() const noexcept;
329
331 int getNumThreads() const noexcept;
332
338 ThreadPoolJob* getJob (int index) const noexcept;
339
344 bool contains (const ThreadPoolJob* job) const noexcept;
345
347 bool isJobRunning (const ThreadPoolJob* job) const noexcept;
348
357 bool waitForJobToFinish (const ThreadPoolJob* job,
358 int timeOutMilliseconds) const;
359
363 void moveJobToFront (const ThreadPoolJob* jobToMove) noexcept;
364
368 StringArray getNamesOfAllJobs (bool onlyReturnActiveJobs) const;
369
370private:
371 //==============================================================================
372 Array<ThreadPoolJob*> jobs;
373
374 struct ThreadPoolThread;
375 friend class ThreadPoolJob;
377
378 CriticalSection lock;
379 WaitableEvent jobFinishedSignal;
380
381 bool runNextJob (ThreadPoolThread&);
382 ThreadPoolJob* pickNextJobToRun();
383 void addToDeleteList (OwnedArray<ThreadPoolJob>&, ThreadPoolJob*) const;
384 void stopThreads();
385
386 // Note that this method has changed, and no longer has a parameter to indicate
387 // whether the jobs should be deleted - see the new method for details.
388 void removeAllJobs (bool, int, bool);
389
391};
392
393} // namespace juce
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
An array designed for holding objects.
A special array for holding a list of strings.
The JUCE String class!
Definition juce_String.h:53
static int getNumCpus() noexcept
Returns the number of logical CPU cores.
A task that is executed by a ThreadPool object.
JobStatus
These are the values that can be returned by the runJob() method.
bool isRunning() const noexcept
Returns true if this job is currently running its runJob() method.
bool shouldExit() const noexcept
Returns true if something is trying to interrupt this job and make it stop.
virtual JobStatus runJob()=0
Performs the actual work that this job needs to do.
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.
ThreadPool()
Creates a thread pool based using the default arguments provided by ThreadPoolOptions.
Used to receive callbacks for thread exit calls.
Priority
The different runtime priorities of non-realtime threads.
Definition juce_Thread.h:54
@ normal
The OS default.
Allows threads to wait for events triggered by other threads.
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
JUCE Namespace.
Object withMember(Object copy, Member OtherObject::*member, Other &&value)
Copies an object, sets one of the copy's members to the specified value, and then returns the copy.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
A set of threads that will run a list of jobs.
ThreadPoolOptions withThreadName(String newThreadName) const
The name to give each thread in the pool.
ThreadPoolOptions withNumberOfThreads(int newNumberOfThreads) const
The number of threads to run.
ThreadPoolOptions withDesiredThreadPriority(Thread::Priority newDesiredThreadPriority) const
The desired priority of each thread in the pool.
ThreadPoolOptions withThreadStackSizeBytes(size_t newThreadStackSizeBytes) const
The size of the stack of each thread in the pool.