diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-04-14 13:13:47 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-04-14 13:13:47 +0100 |
commit | f8ee56f6dc8cf7e92c4bfc5930d32b14f634141c (patch) | |
tree | 909ac21da2cd351a98022105bb7c76bc41259a76 /lib/worker.cpp | |
parent | Simplify doWork, add tests for various interface uses (diff) | |
download | ilt-f8ee56f6dc8cf7e92c4bfc5930d32b14f634141c.tar.bz2 ilt-f8ee56f6dc8cf7e92c4bfc5930d32b14f634141c.tar.xz ilt-f8ee56f6dc8cf7e92c4bfc5930d32b14f634141c.zip |
Current thread partakes in work effort while waiting
This will prevent deadlock if the work pool is otherwise busy by ensuring work is always being done
Diffstat (limited to 'lib/worker.cpp')
-rw-r--r-- | lib/worker.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/worker.cpp b/lib/worker.cpp index 7e7f296..45fb6df 100644 --- a/lib/worker.cpp +++ b/lib/worker.cpp @@ -42,3 +42,24 @@ Worker::worker() j->doWork(); } } + +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(); + } +} |