diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-04-14 14:49:52 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-04-14 14:49:52 +0100 |
commit | 70bddc3f8ef3eeb020b4d6114f3abf4ab160003f (patch) | |
tree | f8e83df70e00c9f7f3588638c756ac66a8ab0199 /lib/worker.cpp | |
parent | Externalise a neater definition of TGAHead (diff) | |
parent | Load texture images in Worker (diff) | |
download | ilt-70bddc3f8ef3eeb020b4d6114f3abf4ab160003f.tar.bz2 ilt-70bddc3f8ef3eeb020b4d6114f3abf4ab160003f.tar.xz ilt-70bddc3f8ef3eeb020b4d6114f3abf4ab160003f.zip |
Merge branch 'worker' into assimp
Diffstat (limited to 'lib/worker.cpp')
-rw-r--r-- | lib/worker.cpp | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/lib/worker.cpp b/lib/worker.cpp index fd255c7..45fb6df 100644 --- a/lib/worker.cpp +++ b/lib/worker.cpp @@ -1,27 +1,24 @@ #include "worker.h" -#if __cpp_lib_semaphore -# include "work.h" -# include <algorithm> -# include <iterator> -# include <mutex> +#include <algorithm> +#include <iterator> +#include <mutex> + +Worker Worker::instance; Worker::Worker() : todoLen {0} { std::generate_n(std::back_inserter(threads), std::thread::hardware_concurrency(), [this]() { - return std::thread {&Worker::worker, this}; + return std::jthread {&Worker::worker, this}; }); } Worker::~Worker() { todoLen.release(std::thread::hardware_concurrency()); - std::for_each(threads.begin(), threads.end(), [](auto & th) { - th.join(); - }); } void -Worker::addWork(WorkPtr j) +Worker::addWorkPtr(WorkPtr j) { std::lock_guard<std::mutex> lck {todoMutex}; todoLen.release(); @@ -45,4 +42,24 @@ Worker::worker() j->doWork(); } } -#endif + +void +Worker::assist() +{ + auto job = [this]() { + using namespace std::chrono_literals; + if (todoLen.try_acquire_for(100us)) { + if (std::lock_guard<std::mutex> lck {todoMutex}; todo.size()) { + WorkPtr x = std::move(todo.front()); + if (x) { + todo.pop_front(); + } + return x; + } + } + return WorkPtr {}; + }; + if (auto j = job()) { + j->doWork(); + } +} |