From 5e16ed11bdfd8c38fae4615a04cf59631b8df2c8 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Fri, 8 Sep 2023 04:26:29 +0200 Subject: [PATCH] Workforce: detach terminating threads instead of joining ...which however brings the problem that we can no longer block the destructor of WorkForce by simply joining on all joinable threads (there is a race between testing joinable() and invoking join(), which does not tolerate non-joinable state. There is a second problem: we need to detect and clean-up terminated workers, even for just finding out how many workers are still active. Fortunately doing so also solves the waiting problem in the destructor --- src/vault/gear/work-force.hpp | 16 ++++++++--- wiki/thinkPad.ichthyo.mm | 53 +++++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/vault/gear/work-force.hpp b/src/vault/gear/work-force.hpp index c234d3722..037d95cce 100644 --- a/src/vault/gear/work-force.hpp +++ b/src/vault/gear/work-force.hpp @@ -105,6 +105,7 @@ namespace gear { } ERROR_LOG_AND_IGNORE (threadpool, "defunct worker thread") ////////////////////////////////////////////////////////////////////////////OOO very important to have a reliable exit-hook here!!! + thread::detach(); } activity::Proc @@ -166,10 +167,17 @@ namespace gear { { for (auto& w : workers_) w.emergency.store(true, std::memory_order_relaxed); - for (auto& w : workers_) - if (w.joinable()) - w.join(); - workers_.clear(); + using namespace std::chrono_literals; ///////////////////////7///WIP + do + std::this_thread::sleep_for(10ms); + while (0 < size()); + } + + size_t + size() + { + workers_.remove_if([](auto& w){ return not w.joinable(); }); + return workers_.size(); } private: diff --git a/wiki/thinkPad.ichthyo.mm b/wiki/thinkPad.ichthyo.mm index 8b4fa1aee..5c91e9d5a 100644 --- a/wiki/thinkPad.ichthyo.mm +++ b/wiki/thinkPad.ichthyo.mm @@ -79940,8 +79940,9 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- - + + + @@ -79955,6 +79956,54 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
+ + + + + + + +
    +
  • + Exception wird gefangen im WorkForce-dtor +
  • +
  • + mit Debugger nicht reproduzierbar +
  • +
+

+ Vermutung: zwischen joinable() ≡ true und dem join()-Aufruf hat sich der Thread selber detached() +

+ +
+
+ + + + +

+ entweder ein Thread soll gereaped werden, oder ein Thread verschwindet einfach von der Bildfläche +

+ +
+
+
+ + + + + + + + + + + + + + + +