diff --git a/modules/juce_core/threads/juce_ThreadPool.cpp b/modules/juce_core/threads/juce_ThreadPool.cpp index 460448bc3c..68e498e6c5 100644 --- a/modules/juce_core/threads/juce_ThreadPool.cpp +++ b/modules/juce_core/threads/juce_ThreadPool.cpp @@ -135,6 +135,32 @@ void ThreadPool::addJob (ThreadPoolJob* const job, const bool deleteJobWhenFinis } } +void ThreadPool::addJob (std::function jobToRun) +{ + struct LambdaJobWrapper : public ThreadPoolJob + { + LambdaJobWrapper (std::function j) : ThreadPoolJob ("lambda"), job (j) {} + JobStatus runJob() override { return job(); } + + std::function job; + }; + + addJob (new LambdaJobWrapper (jobToRun), true); +} + +void ThreadPool::addJob (std::function jobToRun) +{ + struct LambdaJobWrapper : public ThreadPoolJob + { + LambdaJobWrapper (std::function j) : ThreadPoolJob ("lambda"), job (j) {} + JobStatus runJob() override { job(); return ThreadPoolJob::jobHasFinished; } + + std::function job; + }; + + addJob (new LambdaJobWrapper (jobToRun), true); +} + int ThreadPool::getNumJobs() const { return jobs.size(); diff --git a/modules/juce_core/threads/juce_ThreadPool.h b/modules/juce_core/threads/juce_ThreadPool.h index 2bb3b6d120..cc3f059a46 100644 --- a/modules/juce_core/threads/juce_ThreadPool.h +++ b/modules/juce_core/threads/juce_ThreadPool.h @@ -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 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 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