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
This commit is contained in:
parent
81cab9a675
commit
5e16ed11bd
2 changed files with 63 additions and 6 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -79940,8 +79940,9 @@ Date:   Thu Apr 20 18:53:17 2023 +0200<br/>
|
|||
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1694016598589" ID="ID_1104497834" MODIFIED="1694016784846" TEXT="schrittweises Erhöhen der Kapazität">
|
||||
<icon BUILTIN="flag-yellow"/>
|
||||
</node>
|
||||
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1694016719052" ID="ID_841238568" MODIFIED="1694016784846" TEXT="Feststellen der aktuellen Auslastung">
|
||||
<icon BUILTIN="flag-yellow"/>
|
||||
<node COLOR="#338800" CREATED="1694016719052" ID="ID_841238568" MODIFIED="1694139706516" TEXT="Feststellen der aktuellen Auslastung">
|
||||
<arrowlink COLOR="#89c79b" DESTINATION="ID_227859054" ENDARROW="Default" ENDINCLINATION="-428;33;" ID="Arrow_ID_103000260" STARTARROW="None" STARTINCLINATION="296;21;"/>
|
||||
<icon BUILTIN="button_ok"/>
|
||||
</node>
|
||||
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1694016740568" ID="ID_574870562" MODIFIED="1694016784847" TEXT="Speicher bereits terminierter Worker bereinigen">
|
||||
<icon BUILTIN="flag-yellow"/>
|
||||
|
|
@ -79955,6 +79956,54 @@ Date:   Thu Apr 20 18:53:17 2023 +0200<br/>
|
|||
<node CREATED="1694133131004" ID="ID_1711800389" MODIFIED="1694133165892" TEXT="sonst bleibt jedwede Exception-Behandlung hängen">
|
||||
<icon BUILTIN="messagebox_warning"/>
|
||||
</node>
|
||||
<node BACKGROUND_COLOR="#fdfdcf" COLOR="#ff0000" CREATED="1694138097733" ID="ID_198925376" MODIFIED="1694138118757" TEXT="Widerspruch zum Thread-Ende per detach()">
|
||||
<icon BUILTIN="broken-line"/>
|
||||
<node CREATED="1694138204919" ID="ID_353705069" MODIFIED="1694138245909" TEXT="nicht beehbarer Race: joinable() ↯ join()">
|
||||
<node CREATED="1694138280757" ID="ID_1470165686" MODIFIED="1694138463589" TEXT="Symptom: sehe zufällig eine invalid-Exception">
|
||||
<richcontent TYPE="NOTE"><html>
|
||||
<head/>
|
||||
<body>
|
||||
<ul>
|
||||
<li>
|
||||
Exception wird gefangen im WorkForce-dtor
|
||||
</li>
|
||||
<li>
|
||||
mit Debugger nicht reproduzierbar
|
||||
</li>
|
||||
</ul>
|
||||
<p>
|
||||
Vermutung: zwischen joinable() ≡ true und dem join()-Aufruf hat sich der Thread selber detached()
|
||||
</p>
|
||||
</body>
|
||||
</html></richcontent>
|
||||
</node>
|
||||
<node CREATED="1694138465164" ID="ID_1130846770" MODIFIED="1694138532410" TEXT="grundsätzlich aber auch ein konzeptioneller Widerspruch">
|
||||
<richcontent TYPE="NOTE"><html>
|
||||
<head/>
|
||||
<body>
|
||||
<p>
|
||||
entweder ein Thread soll gereaped werden, oder ein Thread verschwindet einfach von der Bildfläche
|
||||
</p>
|
||||
</body>
|
||||
</html></richcontent>
|
||||
</node>
|
||||
</node>
|
||||
<node COLOR="#5b280f" CREATED="1694138534943" ID="ID_1131160592" MODIFIED="1694139586481" TEXT="Lösung per join()">
|
||||
<icon BUILTIN="button_cancel"/>
|
||||
<node CREATED="1694138546802" ID="ID_1142646153" MODIFIED="1694138564394" TEXT="Problem: beendete Threads nicht erkennbar"/>
|
||||
<node CREATED="1694138567806" ID="ID_1862534308" MODIFIED="1694138595694" TEXT="⟹ nicht klar wie viele Threads noch aktiv sind"/>
|
||||
<node CREATED="1694138600978" ID="ID_1698300974" MODIFIED="1694138638937" TEXT="⟹ müßte dann selber eine »aktiv«-Flag einführen"/>
|
||||
</node>
|
||||
<node CREATED="1694138649464" ID="ID_21789098" MODIFIED="1694139584363" TEXT="Lösung per detach()">
|
||||
<icon BUILTIN="forward"/>
|
||||
<node CREATED="1694138670160" ID="ID_328887585" MODIFIED="1694138689001" TEXT="Problem: muß am Ende doch irgendwie auf die Threads warten"/>
|
||||
<node CREATED="1694138769195" ID="ID_508739227" MODIFIED="1694138812818" TEXT="⟹ müßte dann aktiv auf joinable() ≡ false warten"/>
|
||||
<node COLOR="#338800" CREATED="1694139634650" ID="ID_227859054" MODIFIED="1694139693900" TEXT="Lösung: sleep-wait auf size() ≡ 0">
|
||||
<linktarget COLOR="#89c79b" DESTINATION="ID_227859054" ENDARROW="Default" ENDINCLINATION="-428;33;" ID="Arrow_ID_103000260" SOURCE="ID_841238568" STARTARROW="None" STARTINCLINATION="296;21;"/>
|
||||
<icon BUILTIN="button_ok"/>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1693841488183" HGAP="-2" ID="ID_671611566" MODIFIED="1693841514321" TEXT="WorkForce_test (Komponententest)" VSHIFT="-3">
|
||||
|
|
|
|||
Loading…
Reference in a new issue