A simplification to the job scheduler from the previous post is to pass the job function to the thread managing the job call, instead of making a shared pointer and capture it in the lambda.
The schedule method changes to:
void Scheduler::schedule(const Job f, long n) {
std::unique_lock<std::mutex> lock(this->mutex);
condition.wait(lock, [this] { return this->count < this->size; });
count++;
std::thread thread{
[this](const Job f, long n) {
std::this_thread::sleep_for(std::chrono::milliseconds(n));
try {
(*f)();
} catch (const std::exception &e) {
this->error(e);
} catch (...) {
this->error(std::runtime_error("Unknown error"));
}
condition.notify_one();
count--;
}, f, n
};
thread.detach();
}