mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-12 22:41:49 +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 <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <optional>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
@ -48,6 +49,7 @@ namespace util {
|
||||||
std::atomic<int> busyWorkers = 0;
|
std::atomic<int> busyWorkers = 0;
|
||||||
std::atomic<uint> jobsDone = 0;
|
std::atomic<uint> jobsDone = 0;
|
||||||
std::atomic<bool> working = true;
|
std::atomic<bool> working = true;
|
||||||
|
supplier<std::optional<T>> jobsSource = nullptr;
|
||||||
bool failed = false;
|
bool failed = false;
|
||||||
bool standaloneResults = true;
|
bool standaloneResults = true;
|
||||||
bool stopOnFail = true;
|
bool stopOnFail = true;
|
||||||
|
|
@ -151,6 +153,10 @@ namespace util {
|
||||||
return working;
|
return working;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setJobsSource(supplier<std::optional<T>>&& source) {
|
||||||
|
jobsSource = std::move(source);
|
||||||
|
}
|
||||||
|
|
||||||
void terminate() override {
|
void terminate() override {
|
||||||
if (!working) {
|
if (!working) {
|
||||||
return;
|
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) {
|
if (failed) {
|
||||||
throw std::runtime_error("some job failed");
|
throw std::runtime_error("some job failed");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue