Browse Source

Added some ThreadPool::addJob methods that take lambdas

tags/2021-05-28
jules 8 years ago
parent
commit
9d692d0d8b
2 changed files with 36 additions and 0 deletions
  1. +26
    -0
      modules/juce_core/threads/juce_ThreadPool.cpp
  2. +10
    -0
      modules/juce_core/threads/juce_ThreadPool.h

+ 26
- 0
modules/juce_core/threads/juce_ThreadPool.cpp View File

@@ -135,6 +135,32 @@ void ThreadPool::addJob (ThreadPoolJob* const job, const bool deleteJobWhenFinis
}
}
void ThreadPool::addJob (std::function<ThreadPoolJob::JobStatus()> jobToRun)
{
struct LambdaJobWrapper : public ThreadPoolJob
{
LambdaJobWrapper (std::function<ThreadPoolJob::JobStatus()> j) : ThreadPoolJob ("lambda"), job (j) {}
JobStatus runJob() override { return job(); }
std::function<ThreadPoolJob::JobStatus()> job;
};
addJob (new LambdaJobWrapper (jobToRun), true);
}
void ThreadPool::addJob (std::function<void()> jobToRun)
{
struct LambdaJobWrapper : public ThreadPoolJob
{
LambdaJobWrapper (std::function<void()> j) : ThreadPoolJob ("lambda"), job (j) {}
JobStatus runJob() override { job(); return ThreadPoolJob::jobHasFinished; }
std::function<void()> job;
};
addJob (new LambdaJobWrapper (jobToRun), true);
}
int ThreadPool::getNumJobs() const
{
return jobs.size();


+ 10
- 0
modules/juce_core/threads/juce_ThreadPool.h View File

@@ -207,6 +207,16 @@ public:
void addJob (ThreadPoolJob* job,
bool deleteJobWhenFinished);
/** Adds a lambda function to be called as a job.
This will create an internal ThreadPoolJob object to encapsulate and call the lambda.
*/
void addJob (std::function<ThreadPoolJob::JobStatus()> job);
/** Adds a lambda function to be called as a job.
This will create an internal ThreadPoolJob object to encapsulate and call the lambda.
*/
void addJob (std::function<void()> job);
/** Tries to remove a job from the pool.
If the job isn't yet running, this will simply remove it. If it is running, it


Loading…
Cancel
Save