mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
add jobsSource to ThreadPool
This commit is contained in:
parent
d5f8fed633
commit
245e040a1c
1 changed files with 22 additions and 0 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <optional>
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
|
|
@ -48,6 +49,7 @@ namespace util {
|
|||
std::atomic<int> busyWorkers = 0;
|
||||
std::atomic<uint> jobsDone = 0;
|
||||
std::atomic<bool> working = true;
|
||||
supplier<std::optional<T>> jobsSource = nullptr;
|
||||
bool failed = false;
|
||||
bool standaloneResults = true;
|
||||
bool stopOnFail = true;
|
||||
|
|
@ -151,6 +153,10 @@ namespace util {
|
|||
return working;
|
||||
}
|
||||
|
||||
void setJobsSource(supplier<std::optional<T>>&& source) {
|
||||
jobsSource = std::move(source);
|
||||
}
|
||||
|
||||
void terminate() override {
|
||||
if (!working) {
|
||||
return;
|
||||
|
|
@ -221,6 +227,22 @@ namespace util {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (jobsSource) {
|
||||
bool jobsAdded = false;
|
||||
std::lock_guard<std::mutex> jobsLock(jobsMutex);
|
||||
while (true) {
|
||||
auto job = jobsSource();
|
||||
if (job.has_value()) {
|
||||
jobs.push(std::move(job.value()));
|
||||
jobsAdded = true;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (jobsAdded) {
|
||||
jobsMutexCondition.notify_one();
|
||||
}
|
||||
}
|
||||
if (failed) {
|
||||
throw std::runtime_error("some job failed");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue