1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-26 02:14:22 +00:00

Added some ThreadPool::addJob methods that take lambdas

This commit is contained in:
jules 2017-08-01 10:54:03 +01:00
parent ab4013e289
commit 9d692d0d8b
2 changed files with 36 additions and 0 deletions

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();

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